【问题标题】:Multiple RewriteRule多重重写规则
【发布时间】:2013-05-04 17:34:02
【问题描述】:

我想在我的 .htaccess 文件中有多个 RewriteRules。

# Enable Rewriting
RewriteEngine on

# Rewrite profile urls
# Input: /user<userId>
# Output: /profile.php?id=<userId>
RewriteRule ^user(\d+)/?$ profile.php?id=$1 [L]

# Rewrite by default to redirect.php
RewriteRule .* redirect.php

每个请求都指向redirect.php

我认为,在第一个 RewriteRule 中使用 [L] 标志,将停止处理规则集。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    如果您想将/user&lt;userId&gt; 重写为/profile.php?id=&lt;userId&gt; 并将其他URL 重写为/redirect.php,那么您可以尝试这两个配置指令:

    RewriteEngine on
    
    RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
    
    RewriteRule .* /redirect.php
    

    或者:

    RewriteEngine on
    
    RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /redirect.php
    

    【讨论】:

      【解决方案2】:

      将以下内容添加到第二个 RewriteRule 有效。

      # Rewrite by default to redirect.php
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule .* redirect.php
      

      但我不确定,为什么需要这样做。

      【讨论】:

      • 1st: 如果请求的文件名不是常规文件,则继续执行规则。 2nd: 如果请求的文件名不是目录,则继续执行规则。
      【解决方案3】:

      [L] 标志仅在当前迭代中停止处理,但 mod_rewrite 重复过程,直到路径不恒定。 在您的情况下,我认为您可以使用此规则:

      RewriteRule ^user\d+$ profile.php
      
      RewriteCond %{REQUEST_URI} !profile.php
      RewriteRule ^.*$ redirect.php
      

      【讨论】:

        猜你喜欢
        • 2013-06-13
        • 1970-01-01
        • 2015-02-15
        • 2017-11-21
        • 2016-02-29
        • 2017-12-06
        • 2011-02-04
        • 2021-02-09
        • 2013-04-08
        相关资源
        最近更新 更多