Rewrite rules with multiple query string - apache

i'm new on htaccess. I'm working on a website with multiple query strings.
I need to rewrite urls of different query strings like:
/path_to_website/file.php?product=var1&country=0&pag=1 to /path_to_website/var1/
/path_to_website/file.php?product=var1&country=0&pag=var2 to /path_to_website/var1/var2
/path_to_website/file.php?product=var1&country=var2&pag=1 to /path_to_website/var1/var2/
/path_to_website/file.php?product=var1&country=var2&pag=var3 to /path_to_website/var1/var2/var3
As you can see, the problem is between 2 and 3 rewrite because they have the same number of parameters (if the problem is this). I've done this on htaccess file:
###BLOCK 1#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
###BLOCK 2#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 3#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 4#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=$3&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/%6/? [R=301,L]
How can I fix this? Because everything works except when I search for product and country !=0 the engine stops on block 2.
Please someone help me because I'm stuck on this problem.
Thanks a lot!
Edit
Thanks #RavinderSingh13 for your answer. I tried with your rules, like this:
RewriteCond %{QUERY_STRING} ^$
############your rule###########
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
The rewrite with this rules is path_to_website/product/, but the page doesn't work with "ERR_TOO_MANY_REDIRECTS". I tried also with ONLY your rule:
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
In this case, the rewrite don't work at all, just print the query string. I also tried with the rew=1 key, like this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/?$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/? [R=301,L]
The rewrite works with path_to_website/product, but when i change the page on 2, the browser url is path_to_website/product/?product=var1&country=0&pag=2. So I insert the rewrite for product/2/, and the rewrite for "file.php?product=var1&country=0&pag=var2" is:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/%4/? [R=301,L]
An so on. I miss something? Thanks a lot for your help :)

Based on your shown samples, could you please try following Rule sets in your .htaccess file. Also please make sure you clear your cache before testing your URLs.
RewriteEngine ON
##For file.php?product=var1&country=0&pag=1
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
##For file.php?product=var1&country=0&pag=var2
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ file.php?product=$1&country=0&pag=$2 [L]
##For file.php?product=var1&country=var2&pag=1
RewriteRule ^([\w-]+)/([\w-]+)/$ file.php?product=$1&country=$2&pag=1 [L]
##for file.php?product=var1&country=var2&pag=var3
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ file.php?product=$1&country=$2&pag=$3 [L]
Edit
I had a GET request with form submit and href with custom paginator. I solved the problem. The problem was on href because i left the query string like "path_to_website/?product=var1&country=var2&page=1" etc. I resolved this replacing the query string href with "/product/country" for pag1 and so on for the other href. On the submit event, i left my custom block 1 and block 3 rules and for paginator href i wrote your rules for rewrite.

Related

Htaccess with multiple rewrite rules

I have a problem with my rewrite rule in htaccess file. This is my code:
#BLOCK 1#
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/$ path_to_website/file.php?product=$1&country=0&pag=pag-1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=pag-1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
#BLOCK 2#
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ path_to_website/file.php?product=$1&country=$2&pag=pag-1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&pag=pag-1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
#BLOCK 3#
RewriteRule ^([\w-]+)/?$ path_to_website/file.php?product=$1&country=0&pag=pag-1 [L]
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ path_to_website/file.php?product=$1&country=0&pag=$2 [L]
RewriteRule ^([\w-]+)/([\w-]+)/$ path_to_website/file.php?product=$1&country=$2&pag=pag-1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ path_to_website/file.php?product=$1&country=$2&pag=$3 [L]
The first two block comes from submit form and the third block comes from an href.
I want to add the following rules without conflict with the other rules:
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ path_to_website/file2.php?product=$1&country=$2&product-details=$3 [L]
edit
Solved the proble, i just add another parameter to the rewrite for details-page:
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+).([\w-]+)$ path_to_website/file2.php?product=$1&country=$2&product-details=$3&place-holder=$4 [L]
Solved the problem, i just add another parameter to the rewrite for details-page:
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+).([\w-]+)$ path_to_website/file2.php?product=$1&country=$2&product-details=$3&place-holder=$4 [L]
Thanks to all :)

.htaccess external & internal rewrite conflict

I'm trying to rewrite a query string to a path, like so:
http://example.com/?p=page1
to
http://example.com/page/page1
The internal redirect works and I can view the page at the second URL but as soon as I try to redirect the first URL to the second, I get a 'Too many redirects' error.
.htaccess:
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
The first two lines are working by themeselves. The addition of the last two lines causes the error.
Your rule is redirecting the uri back it itself that is why you got the redirect error.
You can use %{THE_REQUEST} or %{ENV_REDIRECT_STATUS} variables to avoid Too many redirect error .
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
or
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /\?p=.+ [NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE]

htaccess mod_rewrite - Trailing Slash and loop of redirects

Here is my htaccess code:
RewriteEngine On
RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
I need to make a rewrite rule like this:
http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term
OR if there is a category
http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category
I need also to redirect all the old urls to the new one.
With this rules all that I can obtain is to have the search term and category name joined togheter. In a nutshell it is as if I had always:
http://mywebsite.com/?search=query+term/Category
If I remove all the conditions (RewriteCond), leaving only the rewrite rule with the addition of a trailing slash:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
Reaching the url of my interest in a direct way, the whole thing works. But I will not have the redirect..
So I have tried to set a rule like this:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
But with this rule I get just a loop of redirects.
You can use the following rules :
RewriteEngine on
#1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
RewriteRule ^ /s/%1/%/%2? [NC,L,R]
#2--Redirect from "/?search=foobar" to "/s/foobar" --#
RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
RewriteRule ^ /s/%1? [NC,L,R]
#1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
#2--Rewrite "s/foobar" to "/?search=foobar"--#
RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]

RewriteRule with question marks htaccess

i know this been asked a lot
but i still did not succeeded to do it
i have this in my htaccess
RewriteRule ^embed/([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
i want it to be
RewriteRule ^embed/?v=([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [L]
RewriteRule ^embed/?v=([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [L]
adding ?v=
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/%1([0-9A-Za-z]{12})/(\d+)x(\d+)$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=$1&w=$2&h=$3 [NC,L]
RewriteRule ^embed/%1([0-9A-Za-z]{12})$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=$1 [NC,L]
not working
example
mysite.com/embed/?v=abcde
mysite.com/embed/?v=abcde/6x6
currently it is
mysite.com/embed/abcde
mysite.com/embed/abcde/6x6
so now this work
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1 [L]
i have noticed that i have this in the file
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_CGI_AUTHORIZATION:%1]
RewriteCond %{QUERY_STRING} ^v=(.*)$ [NC]
RewriteRule ^$ /cgi-bin/index_dl.cgi?op=download1&id=%1 [NC,L]
i am still trying to make this to work
RewriteCond %{QUERY_STRING} ^v=(.*)/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=$2&h=$3 [L]
You are not using the rules properly - passing %1 to the rules won't help you, because you're not matching correctly. REQUEST_URI and QUERY_STRING are two different things. RewriteRule is only able to check the REQUEST_URI, and not the QUERY_STRING, as shown in the answer suggested by Panama Jack.
To achieve what you want, you need to use the following instead:
RewriteEngine On
# Check for mysite.com/embed/?v=abcde/6x6
RewriteCond %{QUERY_STRING} ^v=([^&/]+)\/(\d+)x(\d+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed2&file_code=%1&w=%2&h=%3 [L]
# Check for mysite.com/embed/?v=abcde
RewriteCond %{QUERY_STRING} ^v=([^&]+)$ [NC]
RewriteRule ^embed/?$ /cgi-bin/index_dl.cgi?op=video_embed&file_code=%1 [L]
Here, we simply check the query string for each case. If there is a match, only rewrite if we're at /embed/ (trailing slash optional - you may remove the question mark if you wish for the trailing slash to be required).

Second Rewriterule not working correctly

I am trying to do two things here..
redirect http://site.com.au/brand.php?pBrand=THENORTHFACE to http://site.com.au/brand/the-north-face (this works fine)
redirect http://site.com.au/listingbrand.php?pBrand=THENORTHFACE to http://site.com.au/brand/the-north-face (this does not work, when redirecting listingbrand.php?pBrand=DOSH or pBrand=ATKM, they both point back to the first rewrite the-north-face).
How do i make the second rewrite work for each brand? Also, is it correct to repeat the rewrites for each brand?
RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
RewriteRule ^brand\.php$ /brand/the-north-face/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/the-north-face/ [R=301,L]
RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
RewriteRule ^brand\.php$ /brand/dosh/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/dosh/ [R=301,L]
RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
RewriteRule ^brand\.php$ /brand/all-the-kings-men/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/all-the-kings-men/ [R=301,L]
You have this rule 3 times:
RewriteRule ^listingbrand\.php$ ...
Which is not using RewriteCond since RewriteCond is only applicable to very next RewriteRule. Actually you don't even need a separate rule since earlier RewriteRule can handle both brand.php and listingbrand.php using OR in regex.
Change your code to this:
RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/the-north-face/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/dosh/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/all-the-kings-men/? [R=301,L,NC]