Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am trying to write an Apache mod_rewrite that can take in information from the subdomain, and after the domain and pass it as a couple of variables to a PHP script.

Here is what I have so far, with this I can either get the subdomain, or the URL arguments.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^([^./]+)\.domain\.com/([a-zA-Z0-9/-]+)$
RewriteCond %1 !=www
RewriteRule ^ redirect.php?subdomain=%1&args=$2

RewriteRule ^/?([a-zA-Z0-9/-]+)$ redirect.php?args=$1 [L]

mod_rewrite is enabled, and we are using a wildcard A-record to point all traffic to this server *.domain.com

Additionally, I do apologize if this is posted elsewhere, I have been trying to solve this off and on for the last month and have not figured it out, or found the answer posted anywhere.

share|improve this question
    
Can you clarify the behavior that you're seeing, and what you expect to see? Looks like your first RewriteCond will never get a match, so I'm guessing just your second RewriteRule is working? –  Shane Madden Dec 14 '11 at 6:51

1 Answer 1

As Shane Madden already mentioned, the first RewriteCond doesn't look like it will match anything. The reason is twofold: firstly that the first . is not escaped and secondly that the %{HTTP_HOST} variable does not contain any slashes so the whole section after domain\.com will not match anything.

RewriteCond %{HTTP_HOST} ^([^./]+)\.domain\.com/([a-zA-Z0-9/-]+)$


I've never used %1 on the left side of a RewriteCond before. Unless you're sure that this works, if you have more problems try removing this line to eliminate it as a possible cause. There also shouldn't be an = sign in there. Simply !www is what you want here. If you do have problems with this line, change it to use the %{HTTP_HOST} variable instead of %1.

RewriteCond %1 !=www


The $2 in the substitution of this RewriteRule has no matching set of brackets in the pattern, hence it will be empty. You will probably want to add a larger pattern to this one. If it only has one matching set of brackets, $1 will be what you want, even if you already have %1 in the substitution. I suspect you will also want to make this /redirect.php... if you're doing this in the configuration files and not in a .htaccess.

RewriteRule ^ redirect.php?subdomain=%1&args=$2

Also, I might add that matching simply ^ is prone to creating loops. Be careful with that one.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.