How to manage multiple RewriteCondition in .htaccess - .htaccess

I have one .htaccess file and I'd like to manage a redirection for path containing a selected string (foo) but I don't want to redirect path containing the previous string followed by another selected string (foo/bar).
Can I manage this case with RewriteCond and RewriteRule directive?

Related

RewriteRule not applyed in .htacces by dynamic string when match rules string word

i have write rule in .htaccess like
RewriteRule ^(.*)-list-(.*) blog-single.php?slug=$1&id=$2
it means i go for url
www.mydomai.com/axz-any-post-list-7
its work file but in slug if any word like list it make problem in RewriteRule Like:
www.mydomai.com/axz-any-list-post-list-7
if i have slug like blog-single.php?slug=axz-any-list-post&id=7 then it broken url
so it any way to match only last word list in .htaccess befor id ?

Case Insenstive URLs with friendly url extension hiding. .htm and .html

How to redirect uppercase urls/links to lower case files with a modrewrite at the same time preserve the friendly urls (hiding .html and .htm) on my website?
Currently using the following rewrite rule to hide the .htm and .html files on my website:
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule !.*\.htm$ %{REQUEST_FILENAME}.htm [L]
The goal is to preserve friendly url rule function above but at same time redirect all urls in my site that have uppercase characters in the file name portion of url to the actual file name which is always lowercase letters. for example several links with uppercase letters including in file name look like this:
http://sub.domain.com/dir1/100007ABCD but want the final url to resolve for search engines like this: http://sub.domain.com/dir1/100007abcd
my current static file names include the .htm extension like this: http://sub.domain.com/dir1/100007abcd.htm
In addition: The new rule needs to also rewrite all .html files to friendly on the website, as i have both .htm and .html files (which currentl rule handles both now), but the .html files don't need to be redirected to lowercase urls
You need to define the rewrite map using Apache's internal tolower function. This can only be done in vhost or server config, and will result in an error if you try to put these directives in an htaccess file:
RewriteEngine On
RewriteMap lowercase int:tolower
Then, in your htaccess file, you can use something like this above any rewrite rules you already have. The redirect rules must be applied before whatever rules you use for friendly url stuff:
# check that the lower case version of the URI is different than the unchanged URI
RewriteCond %{REQUEST_URI} /([^/]+)$
RewriteCond ${lowercase:%1}::%1 !^(.*)::\1$
RewriteRule ^(?:(.+/)|)(.*)$ /$1${lowercase:$2} [L,R=301]
This will redirect any requested URI whose last pathnode (the very last part of the /A/B/C path, e.g. the C) has an upper case letter. I'll explain what these rules do:
RewriteCond %{REQUEST_URI} /([^/]+)$: This condition simply extracts the last pathnode into a backreference, to be later used by the %1
RewriteCond ${lowercase:%1}::%1 !^(.*)::\1$: This condition takes the previously extracted pathnode, creates a string that is the all lower case pathnode, a :: and the pathnode. The regular expression assumes that the lower cased version is different than the unmodified version. This means that there's an uppercase letter in the last pathnode.
RewriteRule ^(?:(.+/)|)(.*)$ /$1${lowercase:$2} [L,R=301]: This rule only gets applied when the previous 2 conditions are met. It extracts the last pathnode (represented by the $2 backreference) and everything, if any, before it (represented by the $1 backreference). Then it calls the "lowercase" function on the last pathnode and 301 redirects the browser.

htaccess redirect parameter into directory

I setup a slightly weird hierarchy where index reads the URI to decide the action instead of using actual subdirectories, example: site.com/base/action/
Im moving over from the old system to this new one, but I cant get the redirects to work. What I need is: /base/?param[]=value to do an external redirect to /base/param/value/
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?)(\[\])?=([^&]+)&?(.*)$
RewriteRule ^/?base/?$ /base/%1/%3/?%4 [L,R=301]
This will also preserve any query string that may be after param[]=value. You can alternatively just clobber the entire query string altogether by removing the %4 at the end of the rule's target.

apache rewrite url for codeigniter

I need to build app and i'm using codeigniter for this.
Company sends me a link in format that cant be changed from their side
Link is like:
someurl.com/sms.php?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper& mssid=1234567892&notcharged=0&date=2011-12-26+23%3A31%3A27&keyword=msg&created=2011-12-26+23%3A31%3A26
How can i rewrite it with .htaccess so the codeigniter gets link in segmented format like
someurl.com/sms/myfunction/12345678/msg15/777/cc/someoper/1234567892/0/2011-12-26+23%3A31%3A27/msg/2011-12-26+23%3A31%3A26
Thanks
Edit.
Spent some hours and tried something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^phone\=([^&]+)\&msg\=([^&]+)\&code\=([^&]+)\&country\=([^&]+)\&oper\=([^&]+)\&mssid\=([^&]+)\&date\=([^&]+)\&keyword\=([^&]+)$
RewriteRule ^test\.php$ /sms/doParse/%1/%2/%3/%4/%5/%6/%7/%8 [R,L]
And then typing url like this:
http://test.airtel.lv/test.php?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper&mssid=1234567892&date=2011-12-26+23%3A31%3A27&keyword=msg
I got this:
http://test.airtel.lv/sms/doParse/12345678/msg15/777/cc/someoper/1234567892/2011-12-26+23%253A31%253A27/msg?phone=12345678&msg=msg15&code=777&country=cc&oper=someoper&mssid=1234567892&date=2011-12-26+23%253A31%253A27&keyword=msg
Why returned url has GET params in the ending ?
And one more question, if that company will change and add extra param to link, the rewriting will be broken ? Then how can it be more universal ?
Try this placing this in your .htaccess file in an appropriate place in your document root:
RewriteEngine on
RewriteRule ^sms.php$ /sms/myfunction/ [L]
RewriteCond %{QUERY_STRING} ^[^=]+=([^&]+)&?(.*)
RewriteRule ^sms/myfunction/(.*)$ /sms/myfunction/$1/%1?%2 [L]
The first RewriteRule changes the /sms.php part of the URI to /sms/myfunction/ and the second rule will append each value in your query string into its own part of the path. So a url like this: http://someurl/sms.php?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9 will have the URI internally rewritten to /sms/myfunction/1/2/3/4/5/6/7/8/9. There's no checks on what the variable names are in the query string, only the value is extracted and appended to the URI path.
If you put that code in your server or vhost config (instead of an htaccess file) add a / in front of the sms in each of the rules so it says ^/sms because rules in the htaccess have the leading slash stripped off when matching against the URI.
just handle the GETs in your controller. it's okay, GET isnt evil.
parse_str($_SERVER['QUERY_STRING'], $_GET);
then you can have a simpler rewrite to handle the special route

How to pass a url path to a Qstring using rewiterules?

I need to go from
/folder/12345/?foo=abc
to /folder/index.php?id=12345&foo=abc
head butting the wall it should be easy but I can't get it to work anybody know the answer of the top of there head
Paul
well i assume you are already inside "folder"
then can just write this code inside your .htaccess file or server config INSIDE THAT FOLDER
RewriteEngine On
RewriteRule ([0-9]+) index.php?id=$1 [QSA,L]
it will match anything with digits and then append it as id parameter, the QSA flag appends every already set url parameters, so you dont loose any (as your foo).
the L flag is important so that no infinite redirect loops occour!
if the folder "folder" doesnt actually exist but you are working one directory level higher (with you .htaccess file) you can do the following:
RewriteEngine On
RewriteRule folder/([0-9]+) index.php?id=$1 [QSA,L]

Resources