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

查看:21
本文介绍了使用简单的 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:

我有一个 .php 文件,我想通过重写规则查看使用 SEO 友好 URL 的内容.

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

还要规范化并防止重复内容,我想将 .php 版本 301 改为 SEO 友好版本.

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.

我怎样才能重新设计它以使其正常工作?或者是否有我需要在 WHM 中切换的配置?

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

谢谢!!

推荐答案

这是你的 .htaccess 的样子:

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.

请记住,自从您使用 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.

它会导致重定向循环,因为您将其重定向到自身,我的代码的不同之处在于我捕获请求,并从那里重定向 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天全站免登陆