0

Is it possible to conditionally include the rewriterule that removes www from a URL only on a live site? I have site.dev and staging.site.com and site.com setup but I only want to have one .htaccess file to keep track of. Currently my .htaccess looks like this:

RewriteEngine On

# remove index.php
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

# remove www
# RewriteCond %{http_host} ^www\.site\.com [NC]
# RewriteRule ^(.*)$ http://site.com/$1 [R=301,NC]

on site.dev and staging.site.com. I uncomment the last two lines in site.com. If I leave the two lines uncommented on site.dev it redirects me to site.com.

UPDATE: per request below, here's the entry in httpd-vhosts.conf for this site:

NameVirtualHost *:80

<Directory "/Users/stubear/Sites/site/dev/html/">
Allow From All
AllowOverride All
</Directory>
<VirtualHost *:80>
    ServerName "site.dev"
    DocumentRoot "/Users/stubear/Sites/site/dev/html"
</VirtualHost>
Stuart McCoy
  • 145
  • 2
  • 8
  • just the one above this that removes index.php. Edited above to add full .htaccess file. – Stuart McCoy Aug 31 '13 at 04:04
  • no, I'm ok with site.dev but currently when I enter this into the address bar it redirects me to site.com. If I comment out these lines in the .htaccess file for my local server version it works as expected. I'd like to just have one .htaccess file to keep track of though, not two that I have to ensure I comment out the rewritecond and rewrite rules that remove www from the site.com. – Stuart McCoy Aug 31 '13 at 04:09
  • is the dev domain `site.dev` like this? Do you have a virtualhost setup or ServerAlias on your WEB Server for it? Look at the `vhosts.conf` file of your WAMP server if you're using WAMP. – Prix Aug 31 '13 at 04:10
  • yeah, I've created a virtual host (Apache on OSX.8) for site.dev that points to a folder in ~/user/sites/site. I realize that my .htaccess SHOULD work but for some reason it is not. – Stuart McCoy Aug 31 '13 at 04:18
  • Can you update your question with your virtualhost for site.dev? And do you have site.com also configured on your home server and does it point to the same folder as your site.dev? – Prix Aug 31 '13 at 04:18
  • Updated above and no, I do not have any entries for site.com. It's hosted on MT not by me. – Stuart McCoy Aug 31 '13 at 04:30
  • Then there is no reason that rule would redirect it to site.com, the condition does not met and the rule does not execute. perhaps you have something like `header("Location http://site.com/");` somewhere in your PHP that redirects you to the site.com? – Prix Aug 31 '13 at 04:31
  • Nope, it's an EE site and the home page is currently pretty much empty with the exception of a "Coming Soon" message while I work on the rest of the site. The only EE tag on the template is a snippet that includes my google analytics tracking code on all my templates. – Stuart McCoy Aug 31 '13 at 04:40
  • Well that rule does not redirect your site either, you can confirm that by using inside your virtualhost `RewriteLog "/Users/stubear/Sites/site/dev/rewrite_log" RewriteLogLevel 9` then you can run `tail -f /Users/stubear/Sites/site/dev/rewrite_log` once you access your site with that rule and you will see that it stops the redirect because the URL does not match `10.0.0.1 - - [31/Aug/2013:01:44:50 --0300] [site.dev/sid#7f3b4736d0d0][rid#7f3b476a13f8/initial] (4) [perdir /Users/stubear/Sites/site/dev/html] RewriteCond: input='site.dev' pattern='^www\\.site\\.com$' [NC] => not-matched` – Prix Aug 31 '13 at 04:46
  • I believe you, I just know that it's not working for me. I'll test a new, clean .dev site tomorrow and if it still persists I'll just have to ensure I manually comment out the .htaccess file on the local version. It's not like I'm editing this file or even moving it between serves all that often anyway. Thanks for your help. – Stuart McCoy Aug 31 '13 at 04:58
  • No problem, well if you have nothing from site.com on virtualhost or .htaccess I can only think that some of the PHP files have a configuration about site.com if there is none then you really have something wrong because that rule would 100% fail to redirect if you access site.dev. – Prix Aug 31 '13 at 05:01
  • Any update? Did you try rewrite logging? It usually really helps. – yakatz Oct 04 '13 at 02:23

0 Answers0