homepage Welcome to WebmasterWorld Guest from
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

 4:59 pm on Jan 30, 2006 (gmt 0)

I'm trying to redirect an URL that looks like this:
to this:

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]



 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.


 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]


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



 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:
The second line above doesn't seem to work then.


 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.



 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.


 11:31 pm on Jan 31, 2006 (gmt 0)


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


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



 10:19 pm on Feb 1, 2006 (gmt 0)

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

I'm at a loss...


 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?



 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]


 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]


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