【问题标题】:Adding additional Apache2 rewrites without infinite redirect添加额外的 Apache2 重写而无需无限重定向
【发布时间】:2021-12-20 02:12:25
【问题描述】:

我目前在.htaccess 文件中有以下内容来重写.php 扩展:

# /var/www/html/help/.htaccess

# To externally redirect /dir/foo.php to /dir/foo excluding POST requests
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

按预期工作。但是我希望添加一些额外的重写,例如将/help/open 重定向到/help/submit。我尝试的一切似乎都以来自 /help/open -> /help/submit 并返回的无限重定向结束。

RewriteRule open.php /help/submit [R=302,NE,L]
RewriteRule submit /help/open.php [L]

有人可以建议我如何在此处实现额外的 URL 重写而不导致无限循环吗?

【问题讨论】:

    标签: php .htaccess mod-rewrite url-rewriting apache2


    【解决方案1】:

    您收到无限循环错误,因为您的规则将目标文件 help/open.php 重定向到自身。您可以在第二条规则中使用END 标志来解决此问题。

    RewriteRule open.php /help/submit [R=302,NE,L]
    RewriteRule submit /help/open.php [END]
    

    注意:END 标志仅适用于 Apache 2.4 或更高版本。如果您使用的是旧版本的 Apache,请改用以下解决方案

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule open.php /help/submit [R=302,NE,L]
    RewriteRule submit /help/open.php [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 2011-05-25
      • 2013-01-25
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多