【问题标题】:Is it possible to have htaccess if else type rewrite conditions and rewrite rules?如果其他类型重写条件和重写规则,是否可以使用 htaccess?
【发布时间】:2009-10-22 23:24:31
【问题描述】:

我正在尝试编写一个基本上可以执行此操作的 htaccess 文件:

if(requested file == "some-file.php" || requested file == "some-file2.php" || requested file == "some-file3.php")
   then Rewrite to redirector.php?uri=some-file.php <- substitute requested file without passing any parameters
else
// existing rewrite conditions from silverstripe
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]

这可以使用 htaccess 吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite apache-config


    【解决方案1】:

    我认为您需要做的就是将 3 个唯一请求的文件作为 3 个RewriteRules 放在顶部,在那些 RewriteCond 的上方:

    RewriteEngine On
    RewriteRule /(some-file.php) http://test.dev/redirector.php?uri=$1 [L]
    RewriteRule /(some-file2.php) http://test.dev/redirector.php?uri=$1 [L]
    RewriteRule /(some-file3.php) http://test.dev/redirector.php?uri=$1 [L]
    
    // rest of Rewrite Stuff
    

    【讨论】:

      【解决方案2】:

      试试这个规则:

      RewriteRule ^(some-file\.php|some-file2\.php|some-file3\.php)$ redirector.php?uri=$1
      

      【讨论】:

        【解决方案3】:

        这是您的解决方案:

        # if files are know, redirect and stop:
        RewriteRule ^(some-file|some-file2|some-file3)\.php$ redirector.php?uri=$1.php [QSA,L]
        
        # files that were not known go here:
        # existing rewrite conditions from silverstripe
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
        

        注意: 我已删除:

        RewriteCond %{REQUEST_URI} ^(.*)$
        

        这是没用的。如果你想测试“非空”,这应该是:

        RewriteCond %{REQUEST_URI} !^$
        

        奥利维尔

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-24
          • 2011-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多