Forum Moderators: phranque

Message Too Old, No Replies

Problem with redirect

using mod_rewrite to redirect a page

         

buguela

2:58 pm on Jul 20, 2007 (gmt 0)

10+ Year Member



I have a page that can be accessed via:

http://www.example.com/start.php?state=NY

I need to redirect with 301 all these access to

http://www.example.com/database.php?state=NY

I tried to use this rule, but it not works:

RewriteRule ^start.php?state=([a-zA-Z]{2})$ /database.php?state=$1 [R=301,L]

Anyone knows what is wrong?

[edited by: jdMorgan at 3:39 pm (utc) on July 20, 2007]
[edit reason] example.com [/edit]

g1smd

8:32 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The Query String is held elsewhere.

Your rule isn't processing it properly.

This question has come up several times in the last few days.

There are several related recent examples.

phranque

9:47 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you can access the query string with the RewriteCond directive and then in a subsequent RewriteRule you can backreference the grouped parts of pattern with %N syntax.

buguela

1:48 pm on Jul 23, 2007 (gmt 0)

10+ Year Member



Sorry, i tried but not worked

g1smd

2:10 pm on Jul 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What did you try?

Provide example code.

buguela

5:37 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



I tried this:

RewriteCond %{SCRIPT_FILENAME} ^start.php$
RewriteCond %{QUERY_STRING} ^state=[a-zA-Z]{2}$
RewriteRule (.*) /database.php?state=$1 [R=301,L]

What is wrong? Or am I going to the wrong way?

jdMorgan

4:06 am on Aug 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's more complex than it needs to be. Try:

RewriteCond %{QUERY_STRING} ^state=[a-z]{2}$ [NC]
RewriteRule ^start\.php$ /database.php [R=301,L]

You can eliminate the RewriteCond entirely if you don't mind rewriting requests with query strings that are blank or that do not contain proper two-letter codes. Query strings are passed through RewriteRules unchanged by default.

Jim

jaw76

5:08 pm on Aug 1, 2007 (gmt 0)

10+ Year Member



Hey.

The RewriteRule directive ignores QUERY_STRING arguments by default. To remedy this, use the Query String Append Flag: QSA. It causes the query string to be retained as it is and appended to the end of the resulting request.

Try this per-server:
RewriteEngine On
# Only redirect if state=NY
RewriteCond %{QUERY_STRING} state=NY
RewriteRule ^/start\.php /database.php [QSA,L,R=301]

And this if you are using a .htacess-file:
RewriteEngine On
# Only redirect if state=NY
RewriteCond %{QUERY_STRING} state=NY
RewriteRule ^start\.php /database.php [QSA,L,R=301]

Change the pattern in the RewriteCond to state if you just want to make sure that state is in the query string but don't care about its value.

This should solve your problem (I hope =))...

J

jdMorgan

11:29 pm on Aug 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jaw76,

There is absolutely no need to use [QSA] in this application. [QSA] is only needed if you wish to create new query string name/value pairs *and* append them to the pre-existing query string. If you do not have a pre-existing query string, you don't need it. If you don't create new name-value pairs in the rule, you don't need it.

If no new query parameters are created by the rule, then the existing query string is passed through mod_rewrite rules unchanged. That was the point of my previous post -- unnecessary complication.

Jim

jaw76

12:38 am on Aug 2, 2007 (gmt 0)

10+ Year Member



You are absolutely right. Thanks for the clarification.