Remove tag= using htaccess (including processing order)

I have the following in my htaccess file:

RewriteEngine On
RewriteBase /
# Remove the design part of the URL
RewriteRule ^design/(.*)$ /$1 [L,NC,R]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} s/+product/search/?tag=([^s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]

The external redirect and forwards was provided to me in this question:

RewriteRule for query string to static URL

I have one more issue. When I use my tag based search button linking to mydomain.com/product/search/?tag=hoodies, it rewrites the URL perfectly to mydomain.com/hoodies/ (as an example). However, when I click on a product (on that search page), it takes me to this URL: mydomain.com/games/alien-gamer/?tag=hoodies. I want to remove the /?tag=hoodies from the end without causing a redirect loop. I tried this (but it caused a loop):

RewriteCond %{QUERY_STRING} ^tag=(.*)$
RewriteRule ^(.*)$ /%1/ [R=301,L]

Any suggestions?


Source: .htaccess

Leave a Reply