Reputation: 300
I've been looking around (both here and the googles), and I can't find an answer that actually works for me. Basically, I have two physical servers (a dev server and a production server), and I want to be able to use one .htaccess file in my git repo.
Normally this wouldn't be difficult, but I have two domains, example.com
and longexample.com
. I set up longexample.com
and dev.longexample.com
's CNAMEs to example.com
(and dev.example.com
) at my DNS host, and set the apache ServerName/ServerAlias on both servers:
dev server:
ServerName dev.example.com
ServerAlias dev.longexample.com
prod server:
ServerName example.com
ServerAlias longexample.com
ServerAlias www.example.com
ServerAlias www.longexample.com
Here's my .htaccess file:
Options +FollowSymlinks +Indexes
RewriteEngine on
# RewriteCond %{HTTP_HOST} ^www.longexample.com$ [NC,OR]
# RewriteCond %{HTTP_HOST} ^longexample.com$ [NC,OR]
# RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [R,N]
RewriteCond %{HTTP_HOST} ^dev\.longexample\.com$ [NC]
RewriteRule ^(.*)$ http://dev.example.com/$1 [R,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{THE_REQUEST} ^GET\ ([^\.]+)\.(html|htm) [NC]
RewriteRule ^([^\.]+)\.(html|htm)$ %1 [R,NC,L]
Supposedly the first two blocks should rewrite prod and dev to the proper urls (I'll make them 301s when everything works), but it doesn't seem to be working. Like it is now, it does nothing. If I set the RewriteRule to [N]
instead of [L]
, it gives me an infinite loop.
The first block is commented out because if I get it working on the dev server, the solution for prod should be readily apparent. The last block makes a request to example.com/about.html
redirect to example.com/about
(while still using the /about.html
file. That part works fine.
Is there some issue with using two domains? I've also tried using %{SERVER_NAME}
instead of %{HTTP_HOST}
, but nothing changes.
Upvotes: 1
Views: 227
Reputation: 300
Aha, figured it out! It seems having a chain of [OR]
's messes it up. Here's what works:
Options +FollowSymlinks +Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoshokatana\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^yoshokatana\.com$ [NC]
RewriteRule ^(.*)$ http://yosho.me/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.yosho\.me$ [NC]
RewriteRule ^(.*)$ http://yosho.me/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^dev\.yoshokatana\.com$ [NC]
RewriteRule ^(.*)$ http://dev.yosho.me/$1 [R,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{THE_REQUEST} ^GET\ ([^\.]+)\.(html|htm) [NC]
RewriteRule ^([^\.]+)\.(html|htm)$ %1 [R=301,NC,L]
Upvotes: 1