清理 URL 重定向循环 [英] Clean URL Redirect Loop

查看:38
本文介绍了清理 URL 重定向循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create clean URLs on my website. Now I succeeded in configuring apache so every request like mysite.com/page will work. But I also want that requests for mysite.com/page.php will 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:

<IfModule mod_rewrite.c>
Options +FollowSymlinks

RewriteEngine On
RewriteBase /

# set the variable
RewriteRule ^$ - [L]
RewriteRule ^(.*)$ $1 [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 ^(.*)$ $1.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$ $1 [R=301,L]

谢谢!

推荐答案

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

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

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

这篇关于清理 URL 重定向循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆