【问题标题】:Redirect to correct domain name in .htaccess重定向到 .htaccess 中的正确域名
【发布时间】:2011-03-14 10:56:22
【问题描述】:

我喜欢 Paul Irish 的 HTML5 Boilerplate。我已将 .htaccess 文件中的大部分内容合并到我的文件中。

我喜欢它重定向到域的非 www 版本以及在它丢失时添加尾部斜杠的方式:

# ----------------------------------------------------------------------
# Suppress or force the "www." at the beginning of URLs
# ----------------------------------------------------------------------
# The same content should never be available under two different URLs - especially not with and
# without "www." at the beginning, since this can cause SEO problems (duplicate content).
# That's why you should choose one of the alternatives and redirect the other one.
# By default option 1 (no "www.") is activated. Remember: Shorter URLs are sexier.
# no-www.org/faq.php?q=class_b
# ----------------------------------------------------------------------
# Option 1:
# Rewrite "www.domain.com -> domain.com" 
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteCond %{HTTP_HOST} ^m\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
# ----------------------------------------------------------------------


# ----------------------------------------------------------------------
# Add/remove trailing slash to (non-file) URLs
# ----------------------------------------------------------------------

# Google treats URLs with and without trailing slashes separately.
# Forcing a trailing slash is usually preferred, but all that's really
# important is that one correctly redirects to the other.
# http://googlewebmastercentral.blogspot.com/2010/04/to-slash-or-not-to-slash.html
# http://www.alistapart.com/articles/slashforward/
# http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url Trailing Slash Problem
# ----------------------------------------------------------------------
# Rewrite "domain.com/foo -> domain.com/foo/"

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ /$1/ [R=301,L]
</IfModule>
# ----------------------------------------------------------------------

但是,如果有几个停放的域(或别名)会怎样?例如,如果主域是 www.domain.com,但以下是停放的:www.domain.co.uk 和 www.domain2.com?上面的代码没有考虑到这一点,只会从 www.domain.co.uk 重定向到 domain.co.uk,从 www.domain2.com 重定向到 domain2.com。我希望他们都重定向到 domain.com。理想情况下,我不想将正确的域名放在 .htaccess 文件中,因为那样我就必须单独修改每个站点的 .htaccess 文件。也许这是唯一的方法,因为 .htacess 文件将无法知道正确的域名 - 这是真的吗?我想在每个页面的顶部添加一个简短的 php sn-p 以重定向到正确的域名(无论如何,每个 html 文件都会附加一个简短的配置文件),但这可能不是一个好习惯。

有什么想法吗?

【问题讨论】:

    标签: php apache .htaccess url-rewriting


    【解决方案1】:

    IMO 别名域应该有自己的 VirtualHost,其中只包含 Redirect 语句。 除非您使用基于 IP 的 VHost,否则 VirtualHosts 无论如何都需要包含域名。

    【讨论】:

    • 不完全确定我将如何设置它。我正在使用 WHM/cPanel。这需要为每个域设置一个单独的帐户吗?那会有点痛苦。也许我误解了,因为我对 VirtualHost 一无所知 - '原谅无知!
    • 这很可能是我正在寻找的答案——所以我选择了这个作为答案。不幸的是,我不太明白这将如何在实践中发挥作用。虚拟主机对我来说是新的。如果有人能解释更多,那就太酷了!
    【解决方案2】:

    如果您希望所有域都重定向到同一个域,例如 domain.com,只需 add this to your .htaccess

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^domain.com [NC]
    RewriteRule ^(.*)$ http://domain.com%{REQUEST_URI} [R=301,L]
    

    【讨论】:

    • 谢谢,我的旧 .htaccess 文件中确实有类似的东西。但是,正如我在问题中提到的,我试图避免这种重定向代码,因为这意味着我必须将实际域名添加到每个 .htaccess 文件中。我正在为我创建的每个网站寻找“开箱即用”的 .htaccess 文件,而 html5 样板 .htaccess 文件就是这样做的。
    • @baritoneuk 显然没有其他办法。您可以编辑服务器的vhosts.conf 并将所有域作为别名添加到主域(如ThiefMaster 建议的那样),或者将我给您的sn-p 复制/粘贴到每个域的.htaccess 文件中。域没有“开箱即用”的方式可以知道重定向到哪里。
    • 感谢您澄清这一点 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2011-08-19
    • 2017-04-11
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2014-04-06
    相关资源
    最近更新 更多