简体   繁体   中英

How to use .htaccess mod rewrite to redirect php url as a string?

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!

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. Eg:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM