【问题标题】:.htaccess redirect all but not two.htaccess 重定向所有但不是两个
【发布时间】:2022-01-27 21:18:16
【问题描述】:

我正在尝试使用 .htaccess 进行这样的重定向:

* -> index.html
/affiliate -> affiliate.html
/ref/* -> ref.html

但问题是,所有链接都被重定向到索引,我怎样才能从这条规则中排除几个链接?

我现在有这个代码:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

【问题讨论】:

    标签: .htaccess mod-rewrite apache2


    【解决方案1】:

    使用您显示的示例/尝试,请尝试遵循 htaccess 规则文件。

    请确保在测试您的 URL 之前清除您的浏览器缓存。

    RewriteEngine ON
    ##Rules for uris which starts from affiliate rewrite to affiliate.html here..
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^affiliate/?$ affiliate.html [QSA,NC,L]
    
    ##Rules for uris which starts from ref rewrite it to ref.html here..
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ref  ref.html [QSA,NC,L]
    
    ##Rules for rest of the non-existing pages to rewrite to index.html here...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.html [L]
    

    【讨论】:

      【解决方案2】:

      改用这样的规则:

      Options -MultiViews
      DirectoryIndex index.html
      
      RewriteEngine On
      
      # Prevent further processing if request maps to a static resource
      RewriteRule ^((index|affiliate|ref)\.html)?$ - [L]
      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule . - [L]
      
      # /affiliate -> affiliate.html
      RewriteRule ^affiliate$ affiliate.html [L]
      
      # /ref/* -> ref.html
      RewriteRule ^ref/ ref.html [L]
      
      # * -> index.html
      RewriteRule . index.html [L]
      

      这里不需要RewriteBase 指令。 (或 &lt;IfModule&gt; 包装器。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 2012-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        相关资源
        最近更新 更多