homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Rewrite: query_string -> page
mod_rewrite redirect query string

 5:02 pm on Mar 16, 2003 (gmt 0)

I've been attempting to use rewrite in a htaccess file to redirect a page like:


After spending a few hours browsing the messages here, reading up on regular expressions and so on, I was finally able to come up with what I required:
rewriteCond %{query_string} p=end
rewriteRule ^page.html$ /index.html? [R,L]

It works, but I'm still thinking what I did may be dodgy - that is, there might be a better way? Something I did have trouble with was it would attach the query string (?p=end) to index.html - however I placed a '?' at the end and that got rid of it. :)

Are there any better ways of doing the redirect from a query string to another page without the query string being attached?




 5:28 am on Mar 17, 2003 (gmt 0)


Your solution looks OK to me!

You may want to escape the period in the "page.html" pattern, since otherwise, it means "any character."

RewriteRule ^page\.html$ /index.html? [R,L]

Also, do you want or need an external redirect? That is, do you want the visitor to see the new URL in his browser's address bar? If not, use

RewriteRule ^page\.html$ /index.html? [L]



 5:55 am on Mar 17, 2003 (gmt 0)

Not tested but does

RewriteRule ^page\.html\?p=end$ /index.html [R,L]



 6:00 am on Mar 17, 2003 (gmt 0)


If I understand your question, then no it doesn't, because the query string is not available to the RewriteRule; It has already been moved to the QUERY_STRING environment variable by the time mod_rewrite is activated in a per-directory .htaccess context.



 7:25 am on Mar 17, 2003 (gmt 0)

I've always worked the other way round with mod_rewrite - sending static urls to dynamic contents...

So without the rewrite condition grabbing the query string - would the simple one liner work?

(Don't have access to a mod_rewrite equiped server atm :()


 8:12 am on Mar 17, 2003 (gmt 0)

hi guys,

i'm very interested in this discussion because i am half way through developing a cgi script for a large directory and i want the site to look as if it is totally static html.

is it possible to use static looking urls e.g. www.a.com/widget/red.html , rewrite them to www.a.com/cgi-bin/my.cgi?product=widget&color=red, then dynamically generate the html but leaving www.a.com/widget/red.html in the browser bar? ie the entire site looks static in the browser address bar and also in all of the links on each web page?


 9:36 am on Mar 17, 2003 (gmt 0)

Just to have understood it completely correct:

the first possibilty

RewriteRule ^page\.html$ /index.html? [R,L]

is an internal redirect where I can see the required domain in the adress bar,
the second one
RewriteRule ^page\.html$ /index.html? [L]

redirects to the main domain.

Where should I implement the kind of redirect, i.e. 301 or whatever? Should it be in the brackets, too?
(Of course, only in the first solution: [R=301,L] - correct?)


 12:26 pm on Mar 17, 2003 (gmt 0)

The [R] flag forces an external redirect. The response code is specified in the square brackets: [R=301]. If you use the [R] flag the substitution argument needs to be a valid URI.

So the first RewriteRule [httpd.apache.org] does an external redirect while the second rule will just do an internal rewrite.

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