Solved

htaccess chaining rules with variables

Posted on 2010-08-30
2
460 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

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.

828 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