Htaccess replace characters in request parameter

I have a url like http://www.example.com/?product=test&retailer=test&site=test

In this case (where the product parameter is present) I remove & site = test, but leave the rest of the URL untouched.

If the product parameter is missing, eg. http://www.example.com/?retailer=test&site=test

Am I removing & site = test and change? retailer = test to / retailer / test, so the full url will be http://www.example.com/retailer/test.I also only do this on the root domain.

I use these rules

# first condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^product=([^&\s]+)&retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /?product=%1&retailer=%2 [R=301,L]

# second condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /retailer/%1? [R=301,L]

      

in the second rule, when the url is rewritten to / reatiler / test, there is a possibility for this to be / retailer / test + test in which case i need to change it to / retailer / test -test, it could also be / retailer / test + test + test to be / retailer / test-test-test

help on this would be much appreciated

+1


source to share


1 answer


You can use a flag N

(more details here ):

RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [N,R=301]

      

Note that this is effectively equivalent since redirection is involved here:



RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [R=301,L]

      

So your htaccess should look like this

# first condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^product=([^&\s]+)&retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /?product=%1&retailer=%2 [R=301,L]

# second condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /retailer/%1? [R=301,L]

RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [R=301,L]

      

+1


source







All Articles