Website has changed its url names due to SEO reasons, e.g. it was:
/category/filter1/f00/filter2/123/filter3/100-500/filter4/36.html
now:
/category/color/red/size/big/price/100-500/style/classic.html
I know the old and new names, they’re fixed. Please help me to build a rewrite rule which will result in 301 redirect from old urls to new. I did research and I see that I cannot make it using RewriteMap for example, so I ended up making something like RewriteRule (.*)filter1(.*) $1color$2 [L]
etc. Not only I don’t like the way it looks, but also it doesn’t give me a 301 redirect.
UPDATE: Note that at the moment I have several rules, one per filter name/value, e.g.:
RewriteEngine on
# make sure it's a catalog URL, not anything else
RewriteCond %{REQUEST_URI} !^/(category1|category2|category3|category4)
RewriteRule .* - [L]
# rewrite filter names
RewriteRule (.*)filter1(.*) $1color$2 [L]
RewriteRule (.*)filter2(.*) $1price$2 [L]
...etc...
It works as expected – changing all the names in URL, but setting R
flag causes the stop on first rule and redirect to URL like:
/var/www/vhosts/site/htdocs/category/color/red/filter2/123/ etc...
I separated rules because any of filters may or may not exist in the URL. I will greatly appreciate the better solution.
2
Answers
Here is my own answer: it is possible to do with environment variables. We need to replace old filter names and values with new ones, and then make only one 301 redirect to new URL. Here what I've done using mod_rewrite and environment variables:
Basically, I've reformatted whole the URL in environment variable
filters
then checked if it's a category and not some else part of the website, and finally made redirect to this category+filters variable, appended.html
at the end.Even though the new URL looks prettier to a human, I’m not sure if there’s a need to change the existing URL for SEO reasons.
To get a redirect instead of a rewrite, you must use the
R|redirect
flag. So your rule would look likeBut if you have multiple redirects, this might impact your SEO results negatively, see Chained 301 redirects should be avoided for SEO , but Google will follow 2 or 3 stacked redirects
This means try to replace multiple filters at once
and so on.
When the filters may come in an arbitrary order, you may use several rules and do a redirect at the end
RewriteCond
withREDIRECT_STATUS
is there to prevent an endless loop.When it works as it should, you may replace
R
withR=301
. Never test withR=301
.A final note, be very careful with these experiments. I managed to kill my machine twice (it became unresponsive and I had to switch off) during tests.