I’m using some .htaccess code I found on StackOverflow to redirect non-www domains to their www counterparts:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1
</IfModule>
The problem is that this also works for domains on our local development server, and redirects http://test/ to http://www.test/. This is not useful, as the domain www.test does not exist on our development server. This means that the .htaccess file used for the site in development needs to be modified when the site goes live.
Is there a simple way to add a line to check that the domain name already has at least one dot? If it contains one dot, continue to add the www.. If it doesn’t, stop.
You can add another
RewriteCondbefore yourRewriteRuleto exclude your testserver:I think that an explicit exclusion is better than this dot-count thing.
Edit: Here is the dot-based version: