homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

mod_alias question with query strings
Serving redirects using apache & mod_alias with query strings

 9:09 am on Sep 4, 2002 (gmt 0)

I'm using apache & mod_alias & mod_rewrite and I need to cause a URL like this:


to be served a 301 redirect and be redirected to this URL:


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




 12:19 pm on Sep 4, 2002 (gmt 0)

It might work using mod_rewrite and one of the following RewriteRules:
RewriteRule /test/([0-9]*)-(.*)\.htm(.*)$ [domain.com...] [R=301,L]


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]


 12:58 pm on Sep 4, 2002 (gmt 0)

No the query string is stripped out before any matching takes place. Don't try to match it.

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.


 1:15 pm on Sep 4, 2002 (gmt 0)

Thanks bobriggs.

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.


 1:15 pm on Sep 4, 2002 (gmt 0)

Thanks guys,

But do you know if you can use that %{QUERY_STRING} variable with a mod_alias RedirectMatch 301 line?



 1:35 pm on Sep 4, 2002 (gmt 0)

I just added

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 
Connected to server.
Escape character is '^]'.
GET /rd/?qs HTTP/1.0

HTTP/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

<TITLE>301 Moved Permanently</TITLE>
<H1>Moved Permanently</H1>
The document has moved <A HREF="http://server/rdrd/?qs">here</A>.<P>
<ADDRESS>Apache/1.3.26 Server at server Port 80</ADDRESS>
Connection closed by foreign host.
[root@server conf]#


 1:57 pm on Sep 4, 2002 (gmt 0)

Thanks guys,

I'll get it tested out and will reply to this post when I find out what happens.

Thanks for your help.



 2:25 pm on Sep 4, 2002 (gmt 0)

very strange andreasfriedrich. I cannot duplicate the behavior. Apache 1.3.26 also, but my redirect is in .htaccess, and I can't check with telnet because it goes into the ssh.

But I can check my headers here:

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


 2:45 pm on Sep 4, 2002 (gmt 0)

Must be the difference between having the RedirectMatch directive in httpd.conf and .htaccess.

With the RedirectMatch directive in [server...] and a request to [server...] I get redirected to [server...] No query string there.


 5:12 pm on Sep 4, 2002 (gmt 0)

Hi guys,

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



 6:02 pm on Sep 4, 2002 (gmt 0)


Just a guess, but the httpd.conf redirect is done at a much earlier API phase, perhaps before the query string is moved into an environment variable - which does happen with an .htaccess redirect.

Interesting thread, all... Saved for future reference.



 6:48 pm on Sep 4, 2002 (gmt 0)

Yes, from my standpoint it looks as though (in .htaccess at least), that the QUERY_STRING is passed through using mod_rewrite, but not mod_alias (redirectmatch).

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.


Most of the time I have to play with that mod_rewrite stuff to get it right anyway. ;)


 8:49 pm on Sep 4, 2002 (gmt 0)


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



 11:13 pm on Sep 4, 2002 (gmt 0)

I use mod_rewriteīs feature to add to the query string quite often. It is very useful for your own content negotiation. E.g. you could have an URL like [server...] where en is the desired language and christmas the current website theme (thereīs snow and santa for x-mas, eggs for easter, text for text browsers, etc). In my httpd.conf I have the following RewriteRule:

RewriteRule /([a-z]{2})/([^/])/(*.)$ /$3?lang=$1&theme=$2 [QSA]

Using [QSA] appends to the query string, otherwise it would be substituted.

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