【问题标题】:With mod_rewrite can I specify a RewriteBase within a RewriteCond?使用 mod_rewrite 我可以在 RewriteCond 中指定 RewriteBase 吗?
【发布时间】:2011-01-03 23:48:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:

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

    # 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”的方法。

    【讨论】:

    • 你能弄清楚为什么条件化的RewriteBase 不起作用吗?我现在正在处理同样的问题。
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多