1

I've been trying to make:

http://site.com/file.php?x=foo

redirect to:

http://newsite.com/something/completely/different/

Using the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^file\.php?x=foo$ http://newsite.com/something/completely/different/ [R=301,NC,L]

</IfModule>

But it doesn't do anything (just loads same old page as if I never touched the htaccess).

Is there something I'm missing?

Thanks much for the help!

1 Answer 1

1

The ? at the end of .php is making the last p optional (so .phpx=foo or .phx=foo would match). It's not actually being considered a character in the match. Try escaping the ? like: \?

Edit:

For those wondering what the solution was:

The query string arguments aren't passed to the string that RewriteRule matches against. So you have to create a RewriteCond on QUERY_STRING first, then match the url without arguments. E.g.:

RewriteCond %{QUERY_STRING} x=foo
RewriteRule ^file\.php$ http://newsite.com/something/completely/different/ [R=301,NC,L]

There's probably/hopefully a better way to do this (since this RewriteCond would cascade down to other rules, which is annoying).

Sign up to request clarification or add additional context in comments.

8 Comments

I tried: RewriteRule ^file\.php\?x=foo$ but that doesn't seem to work either.
Nuke the ^ anchor in the front and see if that works. I can't remember which part of the URL mod_rewrite starts matching at. It might be expecting ^/file...
Nope, that was a no-go also. (neither deleting the ^ nor changing it to ^/file... worked)
Are you familiar with vhosts? Are you hitting default and do you know if AllowOverride is set properly?
Also, you can try removing the IfModule tags and see if you get an Error 500 - which would indicate mod_rewrite is not enabled
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.