Solved

Apache Rewrite Rule with Query_String

Posted on 2011-09-22
3
349 Views
Last Modified: 2012-05-12
Hi Experts,

I am trying to force a redirect based on a query parameter.  Let's assume I am redirecting withing the domain test.com.  In plain English I am trying to say:

"If the URL is http://www.test.com/page=94158, then redirect to http://www.test.com/about.htm"

I have this so far:

RewriteCond %{HTTP_HOST} ^(www\.)?test\.com [NC]
RewriteCond %{QUERY_STRING} ^page=94158$
RewriteRule ^(.*)$ http://www.test.com/about.htm [R=302,L]

Open in new window


But my browser is reporting an error that implies that this redirection will create an infinite loop.  

Any suggestions greatly appreciated.
0
Comment
Question by:johnny_f
3 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 36584387
hey Johnny,

I'm answering from my phone so I can't get into detail unfortunately, but I managed to get a url copied at least :)  query_string doesn't work exactly like that it needs var=val, but it could if you rewrote that string, but then you might as well just code a hard rewrite :)  sorry for the format from my phone.  wow that took a long time to type :)
http://wiki.apache.org/httpd/RewriteQueryString
0
 
LVL 13

Accepted Solution

by:
dsmile earned 250 total points
ID: 36586079
@johny: it's because ?page=94158 is automatically appended to the end of about.htm, so your rule will create an infinite loop

Just add ? after .htm on line 3
RewriteCond %{HTTP_HOST} ^(www\.)?test\.com [NC]
RewriteCond %{QUERY_STRING} ^page=94158$
RewriteRule ^(.*)$ http://www.test.com/about.htm? [R=302,L]

Open in new window

0
 

Author Comment

by:johnny_f
ID: 36587095
Hi DSmile,

Bingo - thank you very much.  So close and yet so far.  So that I understand, would it be accurate to say that (a) the default behavior is to append the query string to the end of the new URL so (b) the ? char at the end not only indicates the end of the line, but forces the end of the line?

Thanks for your time Papertrip.

John
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question