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?
käµfm³d 👽Connect With a Mentor 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

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  = )
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.