Solved

htaccess chaining rules with variables

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP temp path missing 4 48
XAMPP 3 44
CentOS 7 (Apache2) Cannot Install SSL Cert 1 46
Problem to get function 52 86
Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now