Remove parenthesis from htaccess (Helicon Ape)

109 Views Asked by At

I'm using Helicon Ape on a Windows Server to create htaccess files.

Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()

RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]

For example in the below URL

http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)

As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".

I tried a few different things that I found in SO but none worked.

So far the closest thing that I could get to working is this:

RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]

The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.

So this would work: http://domain.com/page/11/pag(e-name)

but this wouldn't: http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)

Your assistance is appreciated

Thank You.

2

There are 2 best solutions below

1
Amit Verma On

You can use the following rule :

RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^  %{REQUEST_URI}? [L,R]

If the querystring is dynamic, try:

RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^  %{REQUEST_URI}? [L,R]
0
imvain2 On

Using @starkeen 's example, I was able to create a working solution.

This code handles the Query String separate from the URL. It cleans the URL but removes the query string.

RewriteCond %{REQUEST_URI} [\(\)]+

RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]

RewriteCond %{QUERY_STRING} [\(\)]+

RewriteRule ^ %{REQUEST_URI}? [R=301,L]