views:

376

answers:

2

I'd like to have my .htaccess file specify a different RewriteBase depending on whether the .htaccess file is on my local machine or on a web server. This is the mod_rewrite code I tried, however, it did not work:

RewriteCond %{HTTP_HOST} ^localhost:8080
RewriteBase /example/
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteBase /

This would come in handy for easily previewing several websites on my local machine and then being able to upload those sites to web servers for individual domain names.

+2  A: 

No, that’s not possible with mod_rewrite. But you could use mod_setenvif:

RewriteBase /
SetEnvIf HTTP_Host ^localhost$ local
<IfDefine local>
    RewriteBase /example/
</IfDefine>

But in general you don’t need to specify the base URL path at all as mod_rewrite takes the physical directory path as default.

Gumbo
A: 

The only solution we finally found looks like that :

# Activation of the URL Rewriting
Options +FollowSymlinks
RewriteEngine On

# RewriteBase equivalent - Production
RewriteCond %{HTTP_HOST} !^localhost$
RewriteRule . - [E=REWRITEBASE:/production/path/]

# RewriteBase equivalent - Development
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITEBASE:/development/path/]

# Rewriting
RewriteRule ^(.*)$ %{ENV:REWRITEBASE}index.php?page=$1 [L]

This code offers a way to simulate differents "RewriteBase".

gnutix