URL redirect with & in the string not working in .htaccess

I am trying to redirect the following url but can’t seem to get past the & in the string…


This is what i have in the .htaccess file but it won’t redirect

RewriteCond %{QUERY_STRING} ^url=2199803&am​p;last=capital_safety&rest=Imag​es&ext=$
RewriteRule ^folder/folder/folder/2199803/$ /folder/folder/folder/2199803/2199803-a.jpg? [R=301,L]

Any help would be greatly appreciated… Thanks!

& is only for encoding an ampersand in html, it should not be used in htaccess, it’s not html. I don’t think you need to escape it in htaccess.
When you do need to escape a character in htaccess, use a backslash before it.

The url is showing up in google as a bad link and i want to redirect but can’t get it to work. The url posted by google has the & in the string…

But htaccess is not html, it does not see & as &, it sees it as &.
Try writing the condition as

RewriteCond %{QUERY_STRING} ^url=2199803&last=capital_safety&rest=Imag​es&ext=$

Strange how you only have one & escaped as & and the others are not. Either way, none should be escaped like that in htaccess.

Maybe i am completely missing something… When that url shows up in google search and someone clicks on it it will be a 404 whether i change the & to a & or not…

Do you mean whether or not you change it to & in the htaccess?


Then there must be something else wrong in the rewrite rule or condition.
It’s strange that the string 2199803 is repeated in both the query string and the url path, are you sure that’s correct? Also the last variable ext has no value following the =
Because the query string has no regex ‘wildcards’, so does it need the condition, could you not have the full url in the rule?

It doesn’t need the condition… How would i use the full url in the rule?

If this is the correct URL that people see


and this is the one you want to redirect to


the rule could be

RewriteRule ^mysite.c​om/folder/folder/folder/folder/?url=2199803&last=capital_safety&rest=Imag​es&ext=$ /folder/folder/folder/2199803/2199803-a.jpg? [R=301,L]

Note that & is changed to just &.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.