重定向循环用简单的htaccess的规则 [英] Redirect loop with simple htaccess rule

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

问题描述

我一直在拉我的空出了这一点。它的工作服务器迁移之前!

I have been pulling my air out over this. It worked before the server migration!

好了,所以基本上它是像这样简单:

Ok so basically it's as simple as this:

我有我想要查看通过重写规则使用搜索引擎友好的URL的内容的PHP文件。

I have a .php file that I want to view the content of using a SEO friendly URL via a ReWrite rule.

另外要canonicalise和prevent重复的内容我想301的.php版本的搜索引擎友好的版本。

Also to canonicalise and to prevent duplicate content I want to 301 the .php version to the SEO friendly version.

这是我使用,并一直工作到现在,在新服务器上:

This is what I used and has always worked till now on the new server:

RewriteRule ^friendly-url/$ friendly-url.php [L,NC]
RewriteRule ^friendly-url.php$ /friendly-url/$1 [R=301,L]

然而灾难袭击了,现在它会导致重定向循环。

However disaster has struck and now it causes a redirect loop.

从逻辑上讲,我只能假设,在这个版本的Apache被绊倒了,因为它是看到脚本正在运行的的.php版本,所以它会试图再次重定向。

Logically I can only assume that in this version of Apache it is tripping up as it's seeing that the script being run is the .php version and so it tries the redirect again.

我怎么能重新工作,这使得它的工作?还是有一个配置,我需要在南国切换?

How can I re-work this to make it work? Or is there a config I need to switch in WHM?

谢谢!

推荐答案

这是怎样的的.htaccess 应该是这样的:

This is how your .htaccess should look like:

Options +FollowSymLinks -MultiViews 

RewriteEngine On
RewriteBase /

# To externally redirect /friendly-url.php to /friendly-url/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(friendly-url)\.php [NC]
RewriteRule ^ /%1/? [R=302,L]

## To internally redirect /anything/ to /anything.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/$ $1.php [L]

请注意我如何使用 R = 302 ,因为我不希望规则缓存在我的浏览器,直到我确认其预期的工作,那么,一旦我可以确认它按预期工作,我从 R = 302 切换到 R = 301

Note how I am using R=302, because I don't want the rule to cache on my browser until I confirm its working as expected, then, once I can confirm its working as expected I switch from R=302 to R=301.

请记住,您还可能已经由于您使用的缓存从previous尝试 R = 301 ,让你更好地试图从访问不同的浏览器你已经使用只是为了确保它的工作。

Keep in mind you may have also been cached from previous attempts since you're using R=301, so you better of trying to access it from a different browser you have used just to make sure its working.

然而灾难袭击了,现在它会导致重定向循环。

However disaster has struck and now it causes a redirect loop.

这会导致一个重定向循环,因为您重定向到自己,我的code不同的是,我抓住的请求,并从那里重定向PHP文件,使之友好,然后使用内部重定向。

It causes a redirect loop because your redirecting it to itself, the different on my code is that I capture the request, and redirect the php files from there to make it friendly and then use the internal redirect.

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

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