homepage Welcome to WebmasterWorld Guest from 54.227.215.140
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirecting dynamic pages
siteseo

10+ Year Member



 
Msg#: 5461 posted 4:59 pm on Jan 30, 2006 (gmt 0)

I'm trying to redirect an URL that looks like this:
http://www.domain.com/folder/page.php?q=16
to this:
http://www.domain.com/newfolder/page.php?q=16

I've used the following construction to no avail:
RewriteCond %{QUERY_STRING} ^q\=16$ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]

Straight redirects don't work because of the variable that exists in the first URL.

Any other suggestions for handling redirections on URL's with variables in 'em?

[edited by: jdMorgan at 2:20 am (utc) on Feb. 3, 2006]
[edit reason] De-linked. [/edit]

 

wheel

WebmasterWorld Senior Member wheel us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5461 posted 5:25 pm on Jan 30, 2006 (gmt 0)

This is probably more than you need or are looking for, but what I do is set up apache to have the 404 page go to a script. That script then does a lookup in a mysql database that maps the 'pretty' url to the dynamic url. If it finds the pretty url in the database, it issues a 200 page header and then displays the page. If it doesn't, it leaves the 404 and displays an error.

That allows me to map pages without any consideration for patterns or regular expressions in the URL's. What it doesn't do is map a dynamic page to another dynamic page.

timster

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 5461 posted 5:49 pm on Jan 30, 2006 (gmt 0)

A backreference is what you need:


RewriteCond %{REQUEST_URI} /page.php$
RewriteCond %{QUERY_STRING} ^q=([0-9]+)
RewriteRule ^.*$ /newfolder/page.php?q=%1 [R=301,L]

User Edit -- Added REQUEST_URI condition to make match more specific.

[edited by: timster at 6:02 pm (utc) on Jan. 30, 2006]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5461 posted 5:58 pm on Jan 30, 2006 (gmt 0)

> [...] set up apache to have the 404 page go to a script.

Comment/warning only:

If you use the 404 error handler method, you need to be absolutely sure that the server response code is correct. You cannot allow the 404 error handler to return a 404-Not Found status for pages that *do* exist. Otherwise, you'll trash your search engine rankings.

Use a server headers checker [webmasterworld.com] to be sure...

Jim

siteseo

10+ Year Member



 
Msg#: 5461 posted 7:43 pm on Jan 31, 2006 (gmt 0)

Thanks for the input. I went with this:

RewriteCond %{REQUEST_URI} /page.php$
RewriteCond %{QUERY_STRING} ^q=([0-9]+)
RewriteRule ^.*$ /newfolder/page.php?q=%1 [R=301,L]

Two questions though:
1. How is %1 different from $1
2. What if I have this in the URL:
page.php?q=16+00001
The second line above doesn't seem to work then.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5461 posted 8:34 pm on Jan 31, 2006 (gmt 0)

Simplifying and correcting the query value problem:

RewriteCond %{QUERY_STRING} ^q=[^&]+
RewriteRule ^page\.ph$ http://www.example.com/newfolder/page.php [R=301,L]

As with the original, the redirect will not occur if the query string is blank or if the parameter is missng.

%1 is a back-reference to the first parenthesized value matched in the mosr-recently-matched RewriteCond, whereas $1 is a back-reference to the first parenthesized value matched in the RewriteRule. Neither are required in this case, because query strings are passed through unmodified by default -- unless you have other rules that are interfering.

Also, be aware that the redirect will only occur if the query string starts with "q=".

The pattern [^&]+ requires one or more characters not equal to an ampersand. This is in case you have other query name/value pairs following the "q=" stuff.

Obviously, there are many, many variations possible, so I took my best guess at what you're trying to accomplish.

Jim

siteseo

10+ Year Member



 
Msg#: 5461 posted 10:55 pm on Jan 31, 2006 (gmt 0)

Thanks for the good feedback. In this case though I have to redirect a specific page to another specific page, so using wildcards will probably get me into trouble. I need help with the 16+0001 part of the URL - are the 16 and the 0001 treated as separate components, or as one string of text in this case?

I tried several things, including
RewriteCond %{QUERY_STRING} ^q\=16\+0001
but no luck.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5461 posted 11:31 pm on Jan 31, 2006 (gmt 0)

Try

RewriteCond %{QUERY_STRING} ^q=16\+0001

or

RewriteCond %{QUERY_STRING} ^q=16\%2b0001

The "=" does not need to be escaped. However, the "+" does need to be escaped if it appears as a literal.

Additionally, "+" may appear as an encoded entity in the query string, in which case it will match "%2b". I showed both cases, since I'm not sure how it appears inside %{QUERY_STRING} (please let us know!).

Jim

siteseo

10+ Year Member



 
Msg#: 5461 posted 10:19 pm on Feb 1, 2006 (gmt 0)

Neither
RewriteCond %{QUERY_STRING} ^q=16\+0001
or
RewriteCond %{QUERY_STRING} ^q=16\%2b0001
worked.

I'm at a loss...

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5461 posted 10:50 pm on Feb 1, 2006 (gmt 0)

Let me back up and ask a basic question... Do you have any other working RewriteRules on this server?

Unrelated question: Can you post a line from your raw access log showing a request for this specific file?

Jim

siteseo

10+ Year Member



 
Msg#: 5461 posted 12:08 am on Feb 3, 2006 (gmt 0)

Thanks jdMorgan for your help. This is what finally worked for me:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/page\.php\?q=16 HTTP/
RewriteRule ^folder/page\.php$ http://www.domain.com/newfolder/page.php?q=16 [R=301,L]

[edited by: jdMorgan at 2:20 am (utc) on Feb. 3, 2006]
[edit reason] De-linked [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5461 posted 2:18 am on Feb 3, 2006 (gmt 0)

The crux of this problem was with the "+" in the query string, so a slightly-more accurate depiction of the solution is:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/page\.php\?q=16\+0001 HTTP/
RewriteRule ^folder/page\.php$ http://www.domain.com/newfolder/page.php?q=16+0001 [R=301,L]

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved