【问题标题】:Htaccess Two different redirectsHtaccess 两种不同的重定向
【发布时间】:2008-12-01 17:25:03
【问题描述】:

我有一个可以正常工作的现有 htaccess:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) /default.php 
DirectoryIndex index.php /default.php

我希望对此进行修改,以便所有以 /test/ 开头的 URL 都转到 /test/default.php,同时将所有其他 URL 保留为现有的 /default.php

示例:http://www.x.com/hello.php --> http://www.x.com/default.php 示例:http://www.x.com/test/hello.php --> http://www.x.com/test/default.php

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    将 /test/ 的规则放在其他所有规则之前,但如果匹配,则给它一个 [L] 标志以停止那里的重写规则处理。

    【讨论】:

    • 我试过: RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^test/.* /test/default.php [L] RewriteCond %{SCRIPT_FILENAME} ! -f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule (.*) /default.php DirectoryIndex index.php /default.php 这不起作用。
    【解决方案2】:

    试试这个

    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule (/test/)?(.*) $1/default.php 
    DirectoryIndex index.php /default.php
    

    【讨论】:

    • 这些都不起作用。 x.com/test/hello.php 仍然重定向到 /default,php
    • 您确定删除了其他规则并使用了这条规则吗?
    【解决方案3】:

    这应该可行:

    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule (test/)?(.*) $1default.php [L]
    DirectoryIndex index.php /default.php
    

    【讨论】:

    • 这些都不起作用。 x.com/test/hello.php 仍然重定向到 /default,php
    • 我刚刚在我的开发环境中对其进行了测试,它对我有用。我的解决方案甚至不应该重定向到 /default.php 只是 default.php,如果它重定向到 /default.php 则意味着它调用了 directoryIndex。也许禁用该规则?
    【解决方案4】:

    试试这个。

    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    
    RewriteRule /test/.* /test/default.php 
    RewriteRule .* /default.php 
    
    DirectoryIndex index.php /default.php
    

    您也不需要 (),因为您没有将值设置为变量。

    【讨论】:

      【解决方案5】:

      在您的主文件夹中,使用以下 .htaccess

      RewriteEngine On
      RewriteCond %{SCRIPT_FILENAME} !-f
      RewriteCond %{SCRIPT_FILENAME} !-d
      RewriteRule .* default.php [L]
      DirectoryIndex index.php default.php
      

      在您的./test/ 文件夹中(我假设它是一个物理文件夹),复制并粘贴相同的.htaccess 文件。

      首先,默认开头不需要/。 Apache 将始终假定它与 .htaccess 文件所在的目录相同。

      其次,Apache 在查找.htaccess 文件时会向后查找。所以./test/.htaccess 中的任何内容都会覆盖./.htaccess 中的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 1970-01-01
        • 2019-12-24
        • 2012-01-04
        • 1970-01-01
        相关资源
        最近更新 更多