1
votes

My .htaccess file is in the root, and services multiples websites.

It contains a RewriteEngine block.

I'd like to prevent Apache evaluating rules if the host is not (www) www.example.com:

RewriteEngine On
RewriteBase /

#If this is not about example.com, it is probably about our other websites, exit here
RewriteRule %{HTTP_HOST} !^(www|)example.com$ - [L]

however, this leads to a 500 internal error. Probably due to the usage of {HTTP-HOST} is a RewriteRule directive.

So I'm now thinking about something like this:

RewriteCond %{HTTP_HOST} !^(www|)example.com
RewriteRule (.*) $1 [L]

But this would require an Apache rewrite and is bad for the performance.

Anyone a suggestion?

1

1 Answers

2
votes

I'd like to prevent Apache evaluating rules if the host is not (www) www.example.com

You are using mod_rewrite anyway. The way to do it is skipping the rule if it is not www, so you have to make sure it is.

Something like this should work in the .htaccess file at root directory:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)            /$1            [NC,L]

It's the opposite of what you have tried.

OPTION

Following Arkanon suggestion, this option assumes there are more domains and applies the rule only to the selected one (example.com), with or without www.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(.*)             /$1              [NC,L]