Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mod redirect

Status
Not open for further replies.

kevin197

Programmer
Mar 21, 2002
88
GB
I am trying to use mod redirect for the first time and finding it a bit confusing.

I would like the url page.cgi?part=12345 to become /1/12345.html
and page.cgi?part=GHF426 to /G/GHF426.html

How can I do this with the first char as a directory or would I be better off to just leave the first directory bit off?

I've got this so far

RewriteEngine on
RewriteRule ^page.cgi?part=([]+) $1 [L]
 
A few notes:
the "o" should be a capital "O" in RewriteEngine On

Apache treats anything to the right of the "?" as query string and won't match anything to the right of the "?" with the default RewriteRule. I'm not sure yet how to do the replacement, however you could match the pattern like this.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.cgi$
RewriteCond %{QUERY_STRING} ^part=.*

I'll let you know if I figure out the rewrite of the query.

Rob

Rob Jordan
 
Give this a try. I'm going to test it when I get a chance.

RewriteEngine On
# first pass - this should convert the QUERY to a URI
# I prefixed the request with /query for the second pass
# example - /page.cgi?part=12345 should be converted to /query/part=12345 [R=301,L]
RewriteCond %{REQUEST_URI} ^/page\.cgi$
RewriteCond %{QUERY_STRING} ^part=.*
RewriteRule ^/.* /query/%{QUERY_STRING}

# second pass - now that the request has been converted from a QUERY to URI, we can rewrite it
RewriteRule ^/query/part=(.)(.*) /$1/$1$2 [R=301,L]

Rob

Rob Jordan
 
# added a ? to the end of the first RewriteRule so the original query will be removed from the the redirect URL

RewriteEngine On
# first pass - this should convert the QUERY to a URI
# I prefixed the request with /query for the second pass
# example - /page.cgi?part=12345 should be converted to /query/part=12345 [R=301,L]
RewriteCond %{REQUEST_URI} ^/page\.cgi$
RewriteCond %{QUERY_STRING} ^part=.*
RewriteRule ^/.* /query/%{QUERY_STRING}?

# second pass - now that the request has been converted from a QUERY to URI, we can rewrite it
RewriteRule ^/query/part=(.)(.*) /$1/$1$2 [R=301,L]

Rob Jordan
 
I had a few arguments missing in the previous post.
I've tested the rules below and they appear to work.

RewriteEngine on
#RewriteRule ^/directory/(.*) [R=301,L]
# first pass - this should convert the QUERY to a URI
# I prefixed the request with /query for the second pass
# example - /page.cgi?part=12345 should be converted to /query/part=12345
RewriteCond %{REQUEST_URI} ^/page\.cgi$
RewriteCond %{QUERY_STRING} ^part=.*
RewriteRule ^/page.* /query/%{QUERY_STRING}? [R=301,L]
# second pass - now that the request has been converted from a QUERY to a URI, we can rewrite it
# example: /query/part=12345 should be rewritten to /1/12345
RewriteRule ^/query/part=(.)(.*) /$1/$1$2 [R=301,L]

Rob

Rob Jordan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top