Remove only one GET parameter from url and rewrite the other if no specific GET parameter exists - htaccess
I am trying to set some special conditions for URL rewriting, but I am afraid.
Let's say I have a url like http://www.example.com/?product=test&retailer=test&site=test
In this case (where the parameter is present product
) I just need to remove &site=test
, but leave the rest of the URL untouched.
If the parameter is product
missing, eg.http://www.example.com/?retailer=test&site=test
I still want to remove &site=test
and change ?retailer=test
to /retailer/test
so that the full url is http://www.example.com/retailer/test
. I also want this to happen on the root domain. It's actually hard to even remove a parameter site
...
I've tried this:
RewriteCond %{QUERY_STRING} (?:^|&)sitechange=([^&]+) [NC]
RewriteRule ^$ ? [L,R=301]
but this removes all options in all instances
Also tried the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)?&?site=[^&]+&?(.*)? [NC]
RewriteCond %{REQUEST_URI} /(.*)
RewriteRule .* %3?%1%2? [R=301,L]
but that doesn't do anything. It is very difficult to detect this so that I can find these rewrite rules. If anyone could help I would really love
source to share
Assuming your parameters are sometimes product
and always retailer
and site
(in that order), your rules 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]
source to share
Try with the rules below. I am accepting index.php as a handler and I assume the other constraints are handled by you.
RewriteCond %{QUERY_STRING} ^product=([^/]+)&retailer=([^/]+)&site=([^/]+)$
RewriteRule ^ index.php?product=%1&retailer=%2 [R=301]
RewriteCond %{QUERY_STRING} ^retailer=([^/]+)&site=([^/]+)
RewriteRule ^ /retailer/%1? [R=301]
source to share