How can I write an .htaccess that will rewrite differently based on the request url?
I would like to have one .htaccess file that can rewrite correctly on both my localhost development environment and on my hosted production site. Right now I have to keep two separate file copies for every site I work on. I would like to be able to synchronize the two sites without blowing away either one of their .htaccess files.
Below is the .htaccess file that I'm using with a little pseudo code in the comments to demonstrate what I want to do.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
## IF the url looks like its from localhost then use this rule
## The incoming url might look like: http://localhost/mysite/about
RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
## Else do this rewrite
## The incoming url might look like: http://www.mysite.com/about
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c开发者_运维知识库>
ErrorDocument 404 /index.php
</IfModule>
Here is my server configurations:
Development: xampp on windows
Production: Dreamhost
I'm a bit rusty on this, but I think:
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
#Do your live stuff here
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
#Do your local stuff here
Use RewriteCond to check %{HTTP_HOST}. eg:
RewriteCond %{REMOTE_HOST} ^localhost$
I would just use a separate virtual host for your development environment with the same settings as your production server has.
Just add another <VirtualHost>
container to your httpd.conf or httpd-vhosts.conf configuration file and adjust the settings:
<VirtualHost *:80>
ServerName example.com.local
DocumentRoot /absolute/filesystem/path/to/your/localhost/mysite
</VirtualHost>
精彩评论