简体   繁体   English

.htaccess Apache mod重写规则始终提供重定向循环

[英].htaccess Apache mod rewrite rule always giving a redirect loop

I am normally fine at writing apache redirects but this particular client's server has been giving me problems :c 我通常擅长编写apache重定向,但是这个特定客户端的服务器给我带来了问题:c

I am trying to redirect all sub-pages to the main domain but I am always getting a infinite redirection loop. 我试图将所有子页面重定向到主域,但是我总是遇到无限重定向循环。

Here's my .htaccess file - what am I doing wrong? 这是我的.htaccess文件-我在做什么错?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^test$ "http\:\/\/example\.com\/" [R=301,L]
RewriteCond %{REQUEST_URI} !"^/$"
RewriteRule ^.*$ "http\:\/\/example\.com\/" [R=301,L]

whoa redirect infinity! 哇重定向无限! but say if I do something like this: 但请说,如果我做这样的事情:

RewriteRule ^test$ "http\:\/\/example\.com\/" [R=301,L]

This will redirect http://example.com/test to http://example.com and bam, it works! 这会将http://example.com/test重定向到http://example.com和bam,它起作用了!

Any guidance would be appreciated. 任何指导将不胜感激。 Thanks! 谢谢!

// EDIT //编辑

The issue is that we have a mailing list plugin which sends the user a confirmation email. 问题是我们有一个邮件列表插件,可以向用户发送确认电子邮件。 After clicking the confirmation button in the confirm email the user is redirected back to our site with a long query string appended to http://example.com/ . 单击确认电子邮件中的确认按钮后,用户将被重定向回我们的网站,并在http://example.com/后面附加了一个长查询字符串。 The query string is generated so it's never going to be the same. 查询字符串已生成,因此永远不会相同。

Here's an example of one of the query strings: 这是查询字符串之一的示例:

http://example.com/?has_media=false&fan=https%3A%2F%2Fapp.topspin.net%2Fapi%2Fv1%2Ffan%2Fshow%2F63451204%3Fartist_id%3D12053%26auth%3D96419cab29f59a342cde9ca3c0c20b58&campaign=https%3A%2F%2Fapp.topspin.net%2Fapi%2Fv1%2Fartist%2F12053%2Fcampaign%2F10151864

// Edit 2 //编辑2

I tried @Jon Lin's example and the page loads strangely but it does load. 我尝试了@Jon Lin的示例,页面加载异常,但确实加载了。 Basically, the correct domain loads in some browsers but the assets (CSS, and images do not) and the networks inspector is showing these paths as permanently moved also :s 基本上,某些浏览器会加载正确的域,但资产(CSS和图像不会加载),并且网络检查器也将这些路径显示为永久移动:

Try changing your last rule to: 尝试将您的最后一条规则更改为:

RewriteRule !^(?:index\.(html?|php)|)$ http://example.com/ [R=301,L]

The reason is that after the directory index is mapped (to something like index.php ), the rules get reprocessed again, and thus the extra redirect. 原因是在将目录索引映射到(类似于index.php )目录之后,将重新处理规则,从而进行额外的重定向。

Note that the query string is still going to be at the end. 请注意,查询字符串仍将在末尾。 That's because there is no way to match against the query string in a RewriteRule . 这是因为无法与RewriteRule中的查询字符串进行匹配。 You need to match against the %{QUERY_STRING} variable. 您需要与%{QUERY_STRING}变量匹配。 In order to get rid of the query string, you need to add a ? 为了摆脱查询字符串,您需要添加一个? to the end of http://example.com/ . http://example.com/的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM