Link to home
Start Free TrialLog in
Avatar of eoh-jason
eoh-jason

asked on

How to create an apache mod_rewrite rule

Hello!

Hopefully, this will be simple.

I have a set of conditions requiring me to create an apache mod_rewrite rule to check for certain conditions, and if they are met, to redirect to a different port.

I need to check the following example URL:

http://www.foo.bar/shop/download.php?id=12345

Here are the conditions:

If the request is NOT on port 181
If the request IS download.php
If the requested variable is only id=anything

Here's what I have so far:

RewriteEngine on
ReWriteCond %{SERVER_PORT} !^181$
RewriteCond %{REQUEST_URI} ^/shop/download.php$
RewriteCond %{THE_REQUEST} ^GET\ /shop/download.php\?id=$
RewriteRule ^(.*)$ http://www.foo.bar:181/shop/$1 [R,L]

The above does NOT work.  If I comment out the 3rd line, then anytime download.php is accessed it WILL successfully redirect to port 181, but I need to have it more specific.

Here's the longer explanation:

When someone clicks a link from an email, it takes them to the download page to download purchased items.  There are currently some network issues at my datacenter that are affecting long downloads on port 80.  The cart software will NOT work on anything other than port 80, but the download script WILL work on a nonstandard port.

What I need to do is check the incoming URL and, if it is requesting a download (denoted by the only variable being passed is "id=blahblah") then switch to port 181.  However, the download script can also be called to check for downloads when logged in to the store.  When it is being used in this way, it will have different variables (ie. download.php?user=blah&session=blah.)  So, if the incoming URL has any variables other than or including "id=blah" we need to STAY on port 80.

Does this make sense?

Thanks in advance!

Jason
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eoh-jason
eoh-jason

ASKER

Outstanding!  This worked perfectly.  I'll be posting another question shortly about another rewrite rule, so keep your eyes peeled for another point reward!
NP. Glad to help  = )