使用mod_rewrite,我可以在RewriteCond中指定RewriteBase吗?

时间:2010-01-12 00:19:23

标签: mod-rewrite

我希望我的.htaccess文件指定不同的RewriteBase,具体取决于.htaccess文件是在我的本地计算机上还是在Web服务器上。这是我尝试过的mod_rewrite代码,但它不起作用:

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

这样可以方便地在我的本地计算机上预览多个网站,然后将这些网站上传到各个域名的网络服务器。

1 个答案:

答案 0 :(得分:30)

我们最终找到的唯一解决方案就是:

# 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]

此代码提供了一种模拟不同“RewriteBase”的方法。