apache 清理 URL 重定向循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/499068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:12:20  来源:igfitidea点击:

Clean URL Redirect Loop

apache.htaccessurl-rewriting

提问by prngleo

I'm trying to create clean URLs on my website. Now I succeeded in configuring apache so every request like mysite.com/pagewill work. But I also want that requests for mysite.com/page.phpwill be redirected to mysite.com/page. I've set environment variable in .htaccess to check if i already been redirected to prevent loops, but i still get them... Here's the .htaccess:

我正在尝试在我的网站上创建干净的 URL。现在我成功地配置了 apache,所以像mysite.com/page这样的每个请求都可以工作。但我也希望对mysite.com/page.php 的请求将被重定向到mysite.com/page。我在 .htaccess 中设置了环境变量来检查我是否已经被重定向以防止循环,但我仍然得到它们......这是 .htaccess:

<IfModule mod_rewrite.c>
Options +FollowSymlinks

RewriteEngine On
RewriteBase /

# set the variable
RewriteRule ^$ - [L]
RewriteRule ^(.*)$  [E=REWROTE:0]

# this works fine
# make mysite.com/page work and set REWROTE variable to 1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php [L,E=REWROTE:1]

# but if i enable this, i get redirected and then stuck in a loop
# redirect mysite.com/page.php to mysite.com/page , but only if REWROTE not 1
#RewriteCond %{ENV:REWROTE} !^1$
#RewriteRule ^(.*)\.php$  [R=301,L]

Thanks!

谢谢!

回答by Michael Haren

Add this rule above your existing rewrite rules to stop redirecting if the request has already been redirected once (ref):

将此规则添加到您现有的重写规则之上,以在请求已被重定向一次 ( ref) 时停止重定向:

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

回答by Gumbo

You could check the request line for what has originally been requested:

您可以检查请求行以了解最初请求的内容:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^\ ]*)\.php[?\ ]
RewriteRule \.php$ %1 [R=301,L]

Oh, and the first argument of the RewriteConddirectiveis not a regular expression but just a string. So the escaping the .is wrong.

哦,该RewriteCond指令的第一个参数不是正则表达式,而只是一个字符串。所以逃避.是错误的。

回答by fooquency

Is there some reason why you can't use the end-of-string termination character, $ ?

有什么原因不能使用字符串结束符 $ 吗?

RewriteRule ^/(.+)\.php$ /

will redirect /page.php to /page but will not do any redirecting on /page .

会将 /page.php 重定向到 /page 但不会在 /page 上进行任何重定向。

Fundamentally, using techniques like setting environment variables and adding checks for REDIRECT_STATUS is not going to be very robust.

从根本上说,使用诸如设置环境变量和添加对 REDIRECT_STATUS 的检查之类的技术不会非常健壮。

回答by cazz

another quick & dirty way to prevent looping in these situations i've found is to add a querystring and then check for its existence in the redirect.

在我发现的这些情况下,另一种防止循环的快速而肮脏的方法是添加一个查询字符串,然后在重定向中检查它是否存在。

RewriteCond %{QUERY_STRING} !a=a
RewriteRule ^(.*)\.php$ %1 [NC,R=301,L]
RewriteRule ^(.*)$ .php?a=a [NC,L]

found on this site: http://answers.oreilly.com/topic/542-how-to-properly-redirect-for-maximum-seo/

在此站点上找到:http: //answers.oreilly.com/topic/542-how-to-properly-redirect-for-maximum-seo/