htaccess的问题,重定向没有WWW [英] htaccess problems with redirect for no www

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

问题描述

我一直乱投医管理从www.domain.com重定向到domain.com, 但似乎没有为我工作。 我总是得到一个重定向循环 - 我尝试了,我发现这里还是对谷歌各种事情

I've been trying everything to manage a redirect from www.domain.com to domain.com, but nothing seems to work for me. I always get a redirect loop - and I've tried various things I found here or on Google.

因此​​,这里是我的.htaccess,也许有人可以帮我找出我能做些什么,以正确地重定向或如果有什么错在这里。

So here is my .htaccess, maybe someone could help me figure out what I can do to redirect correctly or if there is something wrong in here.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]

#  Redirect all to .php
#  Example: example.com/hello -> example.com/hello.php
RewriteRule ^(.*)$ $1.php [L,R=301]


# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]

太感谢你了! 我已经花了这么多时间试图弄清楚这一点。

Thank you so much! I've already spent so much time trying to figure this out.

推荐答案

您有一个规则,总是匹配,这是负责的无限重定向。我已经更新了下面的规则集来解决这个问题,并执行你的答案的上方提到的重定向。让我知道如果这样做你所期望的。

You have a rule that always matches, which is responsible for the infinite redirection. I've updated your ruleset below to fix that problem and perform the redirection you mentioned at the top of the answer. Let me know if this does what you expect.

RewriteEngine On

# Redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^.*$ http://example.com/$0 [R=301,L]

# This performs an external redirection? Is that what you want?
# Don't do the rewrite if we're already pointing at a file, otherwise we'll
# just redirect over and over because .* matches what we redirect to, too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}      !\.php$
RewriteRule ^.+$ $0.php [L,R=301]

# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]

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

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