htaccess 没有 www 的重定向问题

发布于 2024-09-08 07:27:32 字数 610 浏览 0 评论 0原文

我一直在尝试一切来管理从 www.domain.com 到 domain.com 的重定向, 但似乎没有什么对我有用。 我总是遇到重定向循环 - 我尝试了在此处或 Google 上找到的各种方法。

这是我的 .htaccess,也许有人可以帮助我弄清楚我可以做什么来正确重定向或者这里是否有问题。

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]

非常感谢! 我已经花了很多时间试图弄清楚这一点。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ花落谁相伴 2024-09-15 07:27:32

您有一个始终匹配的规则,它负责无限重定向。我已经更新了下面的规则集来解决该问题并执行您在答案顶部提到的重定向。让我知道这是否符合您的预期。

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]

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]
世界等同你 2024-09-15 07:27:32

答案是Apache文档,文档告诉我们如何强制www.的使用你只需要把这个例子反过来就可以了。

RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://example.com/$1 [L,R]

The answer is Apache documentation, the documentation tell how to force usage of www. You just have to reverse the example.

RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://example.com/$1 [L,R]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文