Forum Moderators: phranque

Message Too Old, No Replies

Help with RewriteCond/Rewrite rule including query string

         

lat9

11:07 am on Jun 22, 2010 (gmt 0)

10+ Year Member



My site has a bit of a mess right now and I'm trying to find the combination of RewriteCond/RewriteRule statements to pull it all together. Currently, I've got links of the form

www.example.com/pictures?page=[1-4]
www.example.com/pictures?page=[1-4]&language=en
www.example.com/fotos?page=[1-4]&language=en
www.example.com/index.php?main_page=pictures&page=[1-4]

that should all remap to

www.example.com/pictures?id=[1-4]

Is there a "simple" way to accomplish this, especially with the parameter name change from 'page' to 'id'?

lat9

12:23 pm on Jun 22, 2010 (gmt 0)

10+ Year Member



OK, I've got a start:

RewriteCond %{QUERY_STRING} ^.*language=en.*$
RewriteCond %{QUERY_STRING} ^.*page=([0-9]).*$
RewriteRule ^pictures|fotos http://www.example.com/pictures?id=%1 [R=301,L]

That reroutes the language=en&page=? to the English version with the page= renamed to id=.

I guess that I next include a cond/rule group to handle just the index.php version, then another to handle the page= parameter by itself ...

g1smd

1:19 pm on Jun 22, 2010 (gmt 0)

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



None of the .* or $ symbols are needed.

For index.php make sure you cater for both /index.php?param=value and /?param=value here.

lat9

1:36 pm on Jun 22, 2010 (gmt 0)

10+ Year Member



Removing the .* and $ caused the redirect to stop working ...

jdMorgan

4:19 pm on Jun 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteCond %{QUERY_STRING} ^([^&]&)*page=([1-4])(&.*)?$
RewriteRule ^(pictures|fotos|index\.php)$ http://www.example.com/pictures?id=%2 [R=301,L]

The "language=en" name/value pair appears to be irrelevant, since some of your example URLs did not include it.

However, if your URLs may have other non-English language values, and you wish to NOT rewrite those other language requests, then that would require a slightly more complex solution:

# If requested language name/value pair value is "en"
RewriteCond %{QUERY_STRING} ^([^&]&)*language=en(&.*)?$ [OR]
# or if no language name/value pair is present in the request
RewriteCond %{QUERY_STRING} !^([^&]&)*language=[a-z]+(&.*)?$
# and if the page name/value pair is present and the value is 1 to 4 inclusive
RewriteCond %{QUERY_STRING} ^([^&]&)*page=([1-4])(&.*)?$
# Externally redirect requests for /pictures, /fotos, and /index.php to /pictures?id=<page-number>
RewriteRule ^(pictures|fotos|index\.php)$ http://www.example.com/pictures?id=%2 [R=301,L]

Please ignore/omit any "[ smilestopper ]" bbCode tags that appear in the code above. It appears that someone is working on the forum's [ code ] posting function right now.

Jim

lat9

5:40 pm on Jun 22, 2010 (gmt 0)

10+ Year Member



jdMorgan, thanks for the reply. Unfortunately, the language=en is important as there are corresponding language=es parameters for the Spanish-language pages. The main_page parameter can also take on a slew of different values.

What I would up with is this:

# Redirect pages that specify English and the page number
RewriteCond %{QUERY_STRING} ^page=([1-4])&language=en
RewriteRule ^photos|fotos http://www.example.com/photos?id=%1 [R=301,L]

# Redirect pages that specify Spanish and the page number
RewriteCond %{QUERY_STRING} ^page=([1-4])&language=es
RewriteRule ^photos|fotos http://www.example.com/fotos?id=%1 [R=301,L]

# Redirect English-language page w/ renamed parameter
RewriteCond %{QUERY_STRING} ^page=([1-4])
RewriteRule ^photos http://www.example.com/photos?id=%1 [R=301,L]

# Redirect Spanish-language page w/ renamed parameter
RewriteCond %{QUERY_STRING} ^page=([1-4])
RewriteRule ^fotos http://www.example.com/fotos?id=%1 [R=301,L]

# Rewrite the English-language page accessed via index.php
# This one never uses the language parameter
RewriteCond %{QUERY_STRING} ^main_page=photos&page=([1-4])
RewriteRule ^index\.php http://www.example.com/photos?id=%1 [R=301,L]

# Redirect any other combinations of parameters to the
# page, without the parameters
RewriteRule ^photos http://www.example.com/photos? [R=301,L]
RewriteRule ^fotos http://www.example.com/fotos? [R=301,L]

jdMorgan

7:01 pm on Jun 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please note that your RewriteRule pattern is incorrect for the requirements specified in your original post.
It should be "^(photos|fotos)$" with start and end anchors surrounding a parenthesized "OR" clause.

As currently coded, the pattern "^photos|fotos" is taken as meaning "starts with 'photos' OR contains 'fotos', followed by anything or nothing."

Therefore, any URL-path that starts with "photos" will be redirected, and any URL-path which *contains* "fotos" will be redirected.

In both cases, your pattern allows *anything* to follow those two path-parts. So for example, a request for "/photoshop.html" or "/photography.php" will be redirected as well, instead of returning a 404 error as expected.

Jim

lat9

11:25 am on Jun 23, 2010 (gmt 0)

10+ Year Member



Thanks again, jdMorgan. I appreciate your feedback.