TSG
TSG

Reputation: 4617

.htaccess RewriteRule to remove ? following hostname

I have an htaccess file with some rules, and I now want to add another rule which strips a part of the URL such that

www.mydomain.com/?generations/anything

becomes

www.mydomain.com/anything

(and anything means any other characters). I can make it work without the ?, but I can't seem to match/remove the ?. I tried:

1 failed:

RewriteRule ^\?generations/(.*)$ /$1 [L]

2 failed:

RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^generations/(.*)$ $1 [L]

3 failed:

RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^([^?]*)?generations/(.*)$ $2%1 [L]

4 failed:

RewriteRule ^([^?]*)?generations/(.*)$ $1$2 [QSA,L]

Can someone create a working rule - and explain why my third attempt above is not working? I can potentially see problems with the first two, but the third and fourth should work...

Upvotes: 1

Views: 47

Answers (1)

user2493235
user2493235

Reputation:

You're getting there with 3, but the query string is not included in a RewriteRule match. So you just want to match the empty string:

RewriteCond %{QUERY_STRING} ^generations/(.*)$
RewriteRule ^$ %1? [L]

But you also have it the wrong way round I think, if you want /anything to be the URL that is visited in the browser. You actually want just this:

RewriteRule ^(.*)$ ?generations/$1 [L]

Upvotes: 1

Related Questions