How to create an apache mod_rewrite rule


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:

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 ^(.*)$$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!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Instead of:

RewriteCond %{THE_REQUEST} ^GET\ /shop/download.php\?id=$

Open in new window

what about:
RewriteCond %{QUERY_STRING} id=

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eoh-jasonAuthor Commented:
Outstanding!  This worked perfectly.  I'll be posting another question shortly about another rewrite rule, so keep your eyes peeled for another point reward!
käµfm³d 👽Commented:
NP. Glad to help  = )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.