0

I have set up a vHost as follows:

<VirtualHost *:80> ServerAdmin [email protected] ServerName example.com ServerAlias www.example.com DirectoryIndex index.php DocumentRoot /var/www/example.com/www/ <Directory /> Options -FollowSymLinks -Includes -Indexes -MultiViews AllowOverride None </Directory> <Directory /var/www/example.com/www/> <LimitExcept GET POST> deny from all </LimitExcept> Options -FollowSymLinks -Includes -Indexes -MultiViews AllowOverride None Order allow,deny allow from all </Directory> </VirtualHost> 

I would like to add a rewrite rule as follows:

RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC] RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] 

So that i can add a subdomain to my website, but ensure that all traffic to example.com or anything.example.com besides dev.example.com is directed to www.example.com.

Where would i place this rule to make it effective?

I have tried every which way, including placing it in httpd.conf but it seems to either break the vhost config or have no effect.

Any ideas and suggestions would be greatly appreciated.

1 Answer 1

1

Unless this is your default server - IE VirtualHost may be a NameVirtualHost - then apache will only pass requests for example.com and www.example.com to the rewrite rule if its within the given VirtualHost block. If you add anything.example.com to ServerAlias then you might find it works in that context, but just for anything.example.com

To make the rule effective you need to place it in the default VirtualHost block, that is the VirtualHost that appears when you access the server by IP address, not name. On a lot of Apache installs this is handled by a default handler (welcome to Apache style page). Removing that VirtualHost can make this one the default, as can moving this virtualhost further up the config file.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.