Solved

htaccess chaining rules with variables

Posted on 2010-08-30
2
465 Views
Last Modified: 2012-05-10
Hello experts,

I have a series of .htaccess rules that are working well on my Unix server.  I just need to add one extra rule to the chain and I don't know how to do it.  

Currently, for every request that comes in, I strip off the last part of the URL and attach it to a PHP page. Like this:

RewriteRule ^(db)\/(\w*)\/?(\d)?$ index.php?page=$1&dbid=$2&autoplay=$3
RewriteRule ^(db_search)\/(.*)$ index.php?page=$1&search=$2

I also need to add a rule that makes sure there is a www prefix, after all of the other rules have been exercised -- like this:

RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L]

I don't understand how to use the [E] or [ENV] flag to attach those variables ($1,$2,$3) to the last RewriteCond, so that if the entire request came in as http://mydomain/db/abc/123 or mydomain.com/db/abc/123, the URL would ultimately be http://www.mydomain/db/abc/123.

Thank you
-marc
0
Comment
Question by:marcparillo
2 Comments
 
LVL 4

Assisted Solution

by:oldmanbim
oldmanbim earned 250 total points
ID: 33575214
0
 
LVL 26

Accepted Solution

by:
arober11 earned 250 total points
ID: 33577485
Hi, loose the [L]  (Last) from the HTTP_HOST rewrite, and make it the first rule. Any URL modified by that URL will then just drop into your other rules, before possibly being returned to the users browser e.g.

RewriteCond %{HTTP_HOST} !^www.mydomain\.com$ [NC]
RewriteRule .* http://www.mydomain.com%{REQUEST_URI}
RewriteRule ^(db)\/(\w*)\/?(\d)?$ index.php?page=$1&dbid=$2&autoplay=$3
RewriteRule ^(db_search)\/(.*)$ index.php?page=$1&search=$2


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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