Adamski
Adamski

Reputation: 23

htaccess rewrite using wildcard and multiple subdomains

I want to implement a TEST, CERT and PROD webpages on one single webhosting using a single domain. The websites is using wildcards for the first subdomain.

I have the following code:

# for TEST
RewriteCond %{HTTP_HOST} (.*).test.domain.com
RewriteCond %{REQUEST_URI} !^/directory3
RewriteRule ^(.*)$ /directory3/$1 [L]

# for CERT
RewriteCond %{HTTP_HOST} (.*).cert.domain.com
RewriteCond %{REQUEST_URI} !^/directory2
RewriteRule ^(.*)$ /directory2/$1 [L]

# for PROD
RewriteCond %{HTTP_HOST} (.*).domain.com
RewriteCond %{REQUEST_URI} !^/directory1
RewriteRule ^(.*)$ /directory1/$1 [L]

This has 3 problems:
1) The URL is changed in the browser adress bar so the user can see the new URL. I would like the user the see the original types URL
Showing:

http:\\subdomain1.cert.com\directory2

instead of:

http:\\subdomain1.cert.com

2) The TEST and CERT Rewrite is having a Internal Server Error.
It works fine when I replace the wildcard for an fixed subdomain

3) The PROD RweriteCond is always valid (also for the CERT and TEST sub. This means that it is always forwarded to directory1

Upvotes: 0

Views: 3196

Answers (1)

Jon Lin
Jon Lin

Reputation: 143966

The rules are interferring with each other. Because the host: (.*).domain.com also matches the cert and test hosts. You need to exclude them in your "PROD" rule:

RewriteCond %{HTTP_HOST} !test.domain.com
RewriteCond %{HTTP_HOST} !cert.domain.com
RewriteCond %{HTTP_HOST} (.*).domain.com
RewriteCond %{REQUEST_URI} !^/directory1
RewriteRule ^(.*)$ /directory1/$1 [L]

This solves #2 and #3. As for getting redirected, that's something else, These rules won't redirect the browser unless there's an http:// in the target or a R flag in the square brackets.

Upvotes: 1

Related Questions