Forum Moderators: phranque
[domain.com...]
to be served a 301 redirect and be redirected to this URL:
[domain.com...]
I've tried mod_alias using a rule like this:
RedirectMatch 301 /test/([0-9]*)-(.*)\.htm(.*)$ /test/$1.htm$3
But mod_alias is wiping the query string from the URL. I think maybe mod_alias can't handle query strings.
Has anyone had experience of this, or can anyone suggest a way to solve this problem?
Thanks in advance
Anthony
or
RewriteRule /test/([0-9]*)-[^?]*(.*)$ [domain.com...] [R=301,L]
[edited by: heini at 9:21 pm (utc) on Sep. 5, 2002]
[edit reason] fixed sidescrolling caused by preformatting [/edit]
You need to use %{QUERY_STRING} - Only one line for the rewrite, something like:
rewriteRule foo.htm$ bar.htm?%{QUERY_STRING} [R,L]
Substitue foo and bar for your own urls and pattern matches as if the QUERY_STRING weren't there.
I never used mod_rewrite to access the query string before only to build one from the url and wasn´t aware that it was stripped off although that makes sense. How else could you otherwise anchor a re against the end of the url.
You will however need the R=301 flag. Otherwise mod_rewrite will use a temporary redirect 302.
RedirectMatch permanent /rd/ [server...]
to my httpd.conf and requested [server...] and was redirected to [server...] I think that´s exactly what you want.
It seems that with RedirectMatch the query string is transparently passed through. No need for %{QUERY_STRING} which isn´t allowed here anyway.
[root@server conf]# telnet server 80
Trying 192.168.0.10...
Connected to server.
Escape character is '^]'.
GET /rd/?qs HTTP/1.0HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Sep 2002 13:31:07 GMT
Server: Apache/1.3.26 (Unix) mod_ssl/2.8.10 OpenSSL/0.9.6d PHP/4.2.1
Location: [server...]
Connection: close
Content-Type: text/html; charset=iso-8859-1<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>301 Moved Permanently</TITLE>
</HEAD><BODY>
<H1>Moved Permanently</H1>
The document has moved <A HREF="http://server/rdrd/?qs">here</A>.<P>
<HR>
<ADDRESS>Apache/1.3.26 Server at server Port 80</ADDRESS>
</BODY></HTML>
Connection closed by foreign host.
[root@server conf]#
But I can check my headers here:
[searchengineworld.com...]
I put the exact same redirect match into my .htaccess, and servercheck.cgi gives this:
Server Response: [example.com...]
Status: HTTP/1.1 301 Moved Permanently
Date: Wed, 04 Sep 2002 14:21:06 GMT
Server: Apache/1.3.26
Location: [server...]
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
Everythings working they way I wanted it now - this is the rule I used in the end:
RewriteRule test/([0-9]*)-(.*)\.htm(.*)$ test/$1.htm [R=301,L]
My headers:
Server Response: [domain.com...]
Status: HTTP/1.1 301 Moved Permanently
Location: [domain.com...]
It seems we've all got different behavior - but I guess that's what happens with apache & mod_rewrite.
Thanks for all your help
Anthony
I tried antcook's solution on my server and I do not have to append %{QUERY_STRING}, it gets passed through. And it looks like this should also work:
RewriteRule test/([0-9]*)-(.*)\.htm$ test/$1.htm [R=301,L]
(.*) deleted before htm$
And I found an interesting quote on the apache.org site:
One more note: You can even create URLs in the substitution string containing a query string part. Just use a question mark inside the substitution string to indicate that the following stuff should be re-injected into the QUERY_STRING. When you want to erase an existing query string, end the substitution string with just the question mark.
So you could add to or delete whatever's in the QUERY_STRING by using the ? after the sub string.
[httpd.apache.org...]
Most of the time I have to play with that mod_rewrite stuff to get it right anyway. ;)
And thanks for that, too! I had read that bit in the Apache docs, but had no idea what was meant by it. The most valuable thing in the technical world is a good example...
My method when developing my mod_rewrites is to add a few lines at a time, and then go to my error log and examine the 500-Server Error line(s) to see what effect my typos had. ;)
Jim
RewriteRule /([a-z]{2})/([^/])/(*.)$ /$3?lang=$1&theme=$2 [QSA]
Using [QSA] appends to the query string, otherwise it would be substituted.