【问题标题】:Canonical https and www [closed]规范的 https 和 www [关闭]
【发布时间】:2013-04-05 16:01:27
【问题描述】:

抱歉,如果这对某些人来说似乎很简单。

在过去,我使用遍布互联网的 sn-p 代码设置了 301 重定向:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

但是,对于这个特定的项目,有些事情让我不确定。

1) 我使用的是 https,所以现在有四个 CName((CName)是正确的术语吗?) https://example.com https://www.example.com http://example.com http://www.example.com

2) 我正在使用需要 .htaccess 文件的 CMS,以便在运行网站时搜索 CSS 和其他信息。因此,作为一个技术含量较低的人,我担心摆弄 .htaccess 文件(当此文件丢失之前,由于一个单独的问题导致网站无法正常运行)

问题: 1)我将重定向放在他的 .htaccess 文件中的位置是否重要?文件的开始/结束? 2) 我将如何更改上面的 sn-p 以考虑 https?

这是 .htaccess 代码

# Turn on URL rewriting only when mod rewrite is turn on

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Installation directory
    RewriteBase /

    # Protect application and system files from being viewed
    RewriteRule ^(application|modules|system|tests|sql) - [F,L]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Rewrite all other URLs to index.php/URL
    RewriteRule .* index.php?kohana_uri=$0 [L,QSA]
</IfModule>

# Protect the htaccess from being viewed
<Files .htaccess>
    order allow,deny
    deny from all
</Files>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

#Follow symlinks
Options +FollowSymlinks

【问题讨论】:

  • 也许这个answer 会有所帮助。
  • 你想达到什么目的? HTTPS 应该发生什么?
  • @Olaf 我希望访问者访问一个域名,无论他们访问了 4 个变体中的哪一个:example.com

标签: .htaccess mod-rewrite redirect https http-status-code-301


【解决方案1】:

要将所有请求重定向到http://example.com,您可以在规则前加上

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ http://example.com/$0 [R,L]

这会重定向所有带有 HTTPS 或 www.example.com 或两者的请求。

当一切正常时,您可以将R 更改为R=301

永远不要在启用301 的情况下进行测试,有关详细信息,请参阅此答案Tips for debugging .htaccess rewrite rules

【讨论】:

  • 谢谢奥拉夫,这很有帮助。只有一个问题 - 这会被视为 301 重定向吗?据我了解,这很重要。
  • @DougFirr 请查看更新后的答案。
  • 好的,谢谢,我很高兴知道浏览器在使用 301s 时会主动缓存。考虑到这一点,我已经尝试了您的代码,但似乎可能存在问题:如果您访问 https://example.com,它不会重定向。所有其他变体都重定向到 example.com。 SEO眼中的example.com和https://example.com有区别吗?再次感谢你的帮助。如果有帮助,可以在这里看到该网站:tinyurl.com/c8djrvr
  • @DougFirr 我刚刚在我的测试环境中尝试过这个,它会将所有三个http://www.example.comhttps://www.example.comhttps://example.com 重定向到http://example.com。日志文件中是否有任何错误消息或其他内容?
  • 我相信你看到了你的报道!不过,我似乎仍然遇到其中一个问题:example.com 似乎没有重定向到 example.com?
猜你喜欢
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 2011-12-31
  • 2014-03-23
  • 2020-05-07
  • 2012-09-30
  • 2011-09-18
  • 2020-05-22
相关资源
最近更新 更多