Handle query parameters recursively using htaccess - regex

I am here again with the query of re-writing URL :(.
I am using htaccess for re-writing, below is my htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^/([\w\d-]+)/$ [NC]
RewriteRule ^ /?file_name=%1 [L,QSA]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^ %2/?%1%3 [L,R]
Before asking my queries let me give you short explanation about how above code is working. I have a URL like http://www.mysite.com/ (for index) and other pages could be executed by using a parameter called file_name=page_name like http://www.mysite.com/?file_name=my_blog. My htaccess is helping me to exclude the file_name parameter and make a new URL like http://www.mysite.com/my_blog/.
Now I do have a query:
Right now there is already a parameter file_name and now I want to give extra parameters like http://www.mysite.com/?file_name=my_blog&blog_alias=welcome-to-new-generation and wanted this to look like http://www.mysite.com/my_blog/welcome-to-new-generation/. So if I add any number of parameter there parameter name should be removed and only parameter value comes with slash.
Please note: Parameter name could be anything.
Please please please help.

This is a very interesting problem that requires recursive implementation of mod_rewrite rules. Put this code in your .htaccess:
Update II As per further comments, redirects /?file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v URI to /file_name+my_blog/blog_alias+welcome-to-new-generation/foo+bar/n+v
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# forwards ?file_name=my_blog&blog_alias=welcome-to-new-generation to
# /file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?([^\s]+)\s [NC]
RewriteRule ^$ %1? [L]
# redirects /file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v
# to /file_name+my_blog/blog_alias+welcome-to-new-generation/fo+barn+/v
RewriteRule ^(.*/)?([^=]+)=([^&]+)&(.*)$ $1$2+$3/$4 [L]
RewriteRule ^(.*/)?([^=]+)=(.*)$ $1$2+$3 [L,R]
# internal forward from /file_name+my_blog/blog_alias+welcome-to-new-generation to
# /?file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteRule ^([^\+]+)\+([^/]+)/(.*)$ $3?$1=$2 [L,QSA]
RewriteRule ^([^\+]+)\+([^/]+)/?$ /?$1=$2 [L,QSA]
Once you're sure it is working change R to R=301

Related

url-rewriting htacces - how to rewrite my link?

I have been looking all over the internet for this and found many clear documents on how to rewrite a url via htacces. I've gotten so far that htacces is working now, but my specific url(s) won't change. All kind of examples I have tried do not work. Hope someone here can help me with this.
This is my urL :
http://www.stamps-as-a-gift.com/category.php?cat1=Holland&cat2=Water&cat3=Vissen&cat4=Dolfijnen
No I would like it to been seen as:
http://www.stamps-as-a-gift.com/category/Holland/Water/Vissen/Dolfijnen
I like this because it is easier to remember en I have read it's also more SE friendly.
I hope this option is possible for me..thanks!
My code that isnt working:
RewriteEngine on
RewriteRule ^(cat1|cat2|cat3|cat4)/([^/.]+)/([^/.]+)/([^/.]+)$ category.php?cat=$1&cat2=$2&cat3=$3&cat4=$4
Also - do I need to restart apache after every new try ?
You need to use this rule in your root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+category\.php\?cat1=([^\s&]+)&cat2=([^\s&]+)&cat3=([^\s&]+)&cat4=([^\s&]+)\s [NC]
RewriteRule ^ category/%1/%2/%3/%4? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+category\.php\?cat1=([^\s&]+)&cat2=([^\s&]+)&cat3=([^\s&]+)\s [NC]
RewriteRule ^ category/%1/%2/%3? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+category\.php\?cat1=([^\s&]+)&cat2=([^\s&]+)\s [NC]
RewriteRule ^ category/%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+category\.php\?cat1=([^\s&]+)\s [NC]
RewriteRule ^ category/%1/? [R=302,L]
RewriteRule ^category/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ category.php?cat1=$1&cat2=$2&cat3=$3&cat4=$4 [L,QSA,NC]
RewriteRule ^category/([^/]+)/([^/]+)/([^/]+)/?$ category.php?cat1=$1&cat2=$2&cat3=$3 [L,QSA,NC]
RewriteRule ^category/([^/]+)/([^/]+)/?$ category.php?cat1=$1&cat2=$2 [L,QSA,NC]
RewriteRule ^category/([^/]+)/?$ category.php?cat1=$1 [L,QSA,NC]

htaccess redirect rule from www.domain.com/?page=news to www.domain.com/news

i need to redirect the URL as
www.domain.com/?page=news --> www.domain.com/news
Here my htaccess file:
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^http://www\.domain\.com/news/$ /http://www.domain.com/?page=news [L]
Please suggest me the exact rule to use in .htaccess file.
Thanks in advance.
Your 2nd rule is not correct as you can't match domain name in RewriteRule pattern. That pattern only matches REQUEST_URI without domain name and query string.
Your 2nd rule should be like this:
RewriteCond %{THE_REQUEST} \s/+\?page=([^&\s]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Specify search friendly URLs
RewriteCond %{QUERY_STRING} !(^|&)page=[^&]+
RewriteRule ^([^/]+)/?$ /?page=$1 [L,NC,QSA]
Reference: Apache mod_rewrite Introduction
Sorry, I do not know a thing about this, but I have seen a good tutorial posted by someone I am following on facebook:
http://www.9lessons.info/2013/11/htaccess-file-tutorial-and-tips.html
Hope this helps.

301 Redirects: /article_list.php?parent_cat=152&catid=187??? How Do I Redirect This?

I'm setting up 301 Redirects in my .htaccess and 1 of them is:
Redirect 301 /article_list.php?parent_cat=152&catid=187 http://mysitehere.com/resources/objects/tribulus/
When I double check it by going to http://mysitehere.com/article_list.php?parent_cat=152&catid=187 it just stays there on that page and it says 404: Page Not Found. Did I do this incorrectly?
You need mod_rewrite rule to match query string. Consider this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^parent_cat=152&catid=187$ [NC]
RewriteRule ^article_list\.php$ /resources/objects/tribulus/? [R=301,L,NC]
Reference: Apache mod_rewrite Introduction
UPDATE:: Your full .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^parent_cat=152&catid=187$ [NC]
RewriteRule ^article_list\.php$ /resources/objects/tribulus/? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1/ [L]
I believe the Redirect directive of the mod_alias module doesn't really handle/match query strings at all, cf. its documentation:
mod_alias is designed to handle simple URL manipulation tasks. For more
complicated tasks such as manipulating the query string, use the tools provided
by mod_rewrite.
For matching a query string, I suggest you use a RewriteCond+RewriteRule combination, where the RewriteCond matches your %{QUERY_STRING} lexicographically. :)

.htaccess not working only with internet explorer

i have site, that has to rewrite site.ru and www.site.ru to www,site.ru/ru_RU.
I can't access any Apache config files. In htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{HTTP_HOST} ^www.site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's working in firefox or chrome, but in IE i get "this page can't be displayed". Tested on IE10 and IE8 (not compatibility view) on few computers.
If i write some junk in .htacess, i get 500 error in IE. Without .htaccess site loads ok, but i need it to rewrite url. Any ideas how to fix it?
Your flags are all wrong. Modify your rules to this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule !ru_RU /ru_RU [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
Reference: Apache mod_rewrite Introduction
I can't test it now, but here is my guess:
I think http_host ONLY contains the host part, not the uri. So you end up with an endless loop as www.site.ru is will always match again after your rewrite.
You will need another rule that checks if the uri is empty. Like this (untested):
RewriteCond %{REQUEST_URI} ^$
RewriteRule (.*) /ru_RU [QSA]
You might need another condition for the case that the uri contains a slash.

Apache non www to www badly re writting arrays on URL

we have the following .htaccess configuration:
RewriteEngine on
allow from all
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com\/" [R=301,L]
Between other things we want to redirect all non-www URLs to the ones with the www.
Everything works fine, except for the pages where the URL is like this:
index?Form%5bplace%5d=Caribbean&Form%5bdestination%5d=Virgin+Islands&Form%5btype%5d=A
When we enter the URL without the www our redirect ends up with the following URL:
index?Form%255bplace%255d=Caribbean&Form%255bdestination%255d=Virgin+Islands&Form%255btype%255d=A
Which gives an 404 error because it is not recognized.
Any idea how to avoid this?
Replace your code with this:
allow from all
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# if a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php [L]
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteRule ^/?$ http://www.domain1.com/ [R=301,L]
What's happening is that the % symbol is getting escaped to %25.
You can avoid this using the NE flag on your rules