首页 > 解决方案 > .htaccess 应该重定向到指定的站点,但会将其重定向到一些废话重复 wtf 页面。为什么?

问题描述

嘿伙计们,所以今天我又来了一个新问题,问题来了一个问题(希望能回答哈哈)

对,所以我已经下载了PHPApache 2.4。两者都运行完美,我在本地安装了 PHP ( C:\php),所以我确实有 Apache ( C:\Apache24) 作为 Windows 服务运行。当从 localhost 运行时,所有 .php 文件都可以正常工作,显示正确,并且 .htaccess 可以正常工作,直到我最终决定重定向做。

就在那时,奇怪的事情发生了

我将此代码放在根目录的 .htaccess 中:

重定向 301 / /FormTest/register/

是的,我已将 index.php 配置为 index.html - 默认根目录文档,是的,FormTest/register 存在,是的,我尝试将它/FormTest/register/index.html也放在相同的结果中,对于那些会问的人:) )

所以让我们开始吧:它会将我重定向(大约 7 秒(?))到

http://localhost/FormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/registerFormTest/register

(对不起,这就是我真正得到的)

那么,它到底把我重定向到了什么,为什么,它会像这样 - 当然希望如何解决它:D

*I tried researching for answers, no found. I am thinking of => It's taking pretty long so it "requests" to redirect multiple times, when it actually tried to do all those (idk, 7 sec, 7 requests?) redirects, it joins them together and ends up like above*

在此先感谢,任何帮助表示赞赏!

标签: phpapachelocalhost

解决方案


问题是你的Redirect规则:

Redirect 301 / /FormTest/register/

不匹配 URL /,它匹配URL/开头的任何字符。特别是,上述规则旨在重定向/foo//FormTest/register/foo.

你想告诉 Apache 只重定向 URL/而不是其他的,使用RedirectMatch,它允许你匹配 a/作为 URL 中唯一的字符 ($是一个正则表达式符号,意思是“字符串的结尾”,^意思是相反的,“开始细绳”):

RedirectMatch 301 "^/$" "/FormTest/register/"

请记住,这不会重定向/index.html或重定向/index.php到表单测试 URL。您可以添加其他重定向规则来解决此问题:

RedirectMatch 301 "^/$" "/FormTest/register/"
RedirectMatch 301 "^/index\.html$" "/FormTest/register/"
RedirectMatch 301 "^/index\.php$" "/FormTest/register/"

(引号是可选的。)


推荐阅读