.htaccess重定向错误 [英] .htaccess Redirect Error

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

问题描述

我正在尝试将http://www.example.co.ukhttp://example.co.uk重定向到 https://example.co.uk.

I'm trying to redirect http://www.example.co.uk and http://example.co.uk to https://example.co.uk.

.htaccess文件中,我有:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

这将重定向以www开头的那个.但是如何在不发生重定向过多"错误的情况下重定向另一个?

This redirects the one starting with www. but how I redirect the other as well without the "too many redirects" error occuring?

推荐答案

最好是使用两个单独的规则:

Best is to use two separate rules:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^ https://mysite.co.uk%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://mysite.co.uk%{REQUEST_URI} [L,R=301]

和一般提示:您应该始终喜欢将此类规则放置在http服务器(虚拟)主机配置中,而不是使用动态配置文件(.htaccess样式文件).众所周知,这些文件容易出错,难以调试,并且确实降低了服务器的速度.仅当您无法控制主机配置(阅读:真正便宜的托管服务提供商)或者您的应用程序依赖于编写自己的重写规则(这显然是安全的噩梦)时,才提供它们作为最后的选择. ).

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

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

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