MetalMonkey
MetalMonkey

Reputation: 33

.htaccess - Strip characters '%26' from url

I have been a long time reader but this is my first post. Normally I can google and problem solve my way through any issues I have, but this one has taken me many hours and I am at a loss.

On the plus side I have learnt much about the makeup of .htaccess code as opposed to all the cutting and pasting I have been doing all these years.

The issue at hand is that I am moving from an old CMS system to a new one. The old system displayed [&] ampersand characters in the url in their hex format, '%26'. The new CMS system strips this completely. I have been trying to remove this through a rewrite rule.

The objective is to make this url:
http://domain.com.au/drums-%26-percussion/

Redirect to this:
http://domain.com.au/drums-percussion/

I have tried numerous lines and combinations, but these two example are have been the closest I could get to it work.

Example A)
RewriteRule ^(.*)\%26(.*)$ http://domain.com.au/$1-$2 [L,NE,N,R=301]

This above example works if I actually use the &, not the hex format %26. For some reason unknown to me I cannot get it fire. eg, nothing happens. I've tired so many variations around the %26.

Example B)
RewriteCond %{THE_REQUEST} ^(.*)(-%26-)(.*)  [NC]
RewriteRule ^ http://domain.com.au/$1-$2 [L,NE,R=302]

I read somewhere that the %26 had to be put into a condition to be picked up. So I tried this. It reads the %26 and fires off the redirect. The issue is I am not passing on the $1 $2 values. The end result I am getting with this code is:

Start:
http://domain.com.au/drums-%26-percussion/
Redirect:
http://derringers.testpad.com.au/-  

I appreciate any and all advice is this matter. Thank you.

Upvotes: 2

Views: 461

Answers (1)

axiac
axiac

Reputation: 72226

As the documentation states:

On the first RewriteRule, it is matched against the (%-decoded) URL-path of the request.

This means that the URL you see in browser as:

http://domain.com.au/drums-%26-percussion/

is, in fact:

http://domain.com.au/drums-&-percussion/

and the RewriteRule tries to match /drums-&-percussion

Given that, the correct RewriteRule to match the URL is:

RewriteRule ^(.*)-&-(.*)$ http://domain.com.au/$1-$2 [L,NE,N,R=301]

Upvotes: 2

Related Questions