views:

73

answers:

1

Hello.

I want to rewrite a specific URL, I'll show an example so you'll understand what I mean.

First, my current rewrite rule:

RewriteRule ^/?([a-zA-Z0-9/-]+)/?$ /index.php [NC,L]

Now I want this URL:

http://example.tld/foobar?test

Rewritten to:

http://example.tld/foobar

Note: only for /foobar?test! E.g. not for /somethingelse?test and also not for /foobar?blah!

Thanks in advance!

EDIT: I realized I want a 301 redirect from /foobar?test to /foobar, not a "traditional" rewrite. Hope that is possible.

A: 
RewriteCond %{QUERY_STRING}    ^test$
RewriteRule ^/foobar$          /foobar      [NC,R=301,L]
Fabian
Can't get this to work.
@Fabian: You can't redirect to the `REQUEST_FILENAME`! I think you meant `%{REQUEST_URI}`, heh.
Tim Stone
@tshabalala: Also, add a `L` to the flags if you're putting this rule before other rules that might match, or the redirect might be scrapped.
Tim Stone
@Tim Heh ye thanks :D. Btw the `L` becomes obsolete when we add an `R`.
Fabian
@Fabian: No problem! As for the `L` flag, this isn't actually true. All `R` does with a 3xx status code is make a note of the header change and prepend the host information. If you want the redirection to be performed immediately, you need to specify the `L` flag. The `mod_rewrite` documentation makes a note of this under the `R` flag description: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
Tim Stone
@Tim You are right, "To stop rewriting, you should add the 'L' flag." You should post an answer so i can upvote you :D.
Fabian
@Fabian: Nah, I don't think I added anything worth a separate answer, just happy to help! But thanks.
Tim Stone