Welcome to WebmasterWorld Guest from 54.196.231.129

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite: query_string -> page

mod_rewrite redirect query string

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

New User

10+ Year Member

joined:Dec 21, 2002
posts:9
votes: 0


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

/page.html?p=end
to
/index.html

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?

Thanks,
Kurieuo.

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

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


Kurieuo,

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]

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 8, 2001
posts:926
votes: 0


Not tested but does

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

work?

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

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


gethan,

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.

Jim

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 8, 2001
posts:926
votes: 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)

Full Member

10+ Year Member

joined:Feb 5, 2002
posts:333
votes: 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)

Preferred Member

10+ Year Member

joined:Aug 15, 2000
posts:482
votes: 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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members