重定向到 .htaccess 中的正确域名

发布于 2024-10-21 13:12:19 字数 2550 浏览 2 评论 0原文

我喜欢 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 片段以重定向到正确的域名(无论如何,每个 html 文件前面都会添加一个简短的配置文件),但这可能不是一个很好的做法。

有什么想法吗?

I'm loving Paul Irish's HTML5 Boilerplate. I've incoporated much of what's in the .htaccess file into mine.

I like the way it redirects to the non-www version of the domain as well as adding a trailing slash when it's missing:

# ----------------------------------------------------------------------
# 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>
# ----------------------------------------------------------------------

But, what happens if there are a couple of parked domains (or aliases)? For example if the main domain is www.domain.com but the following are parked: www.domain.co.uk and www.domain2.com? The above code doesn't take that into account, and will just redirect from www.domain.co.uk to domain.co.uk and from www.domain2.com to domain2.com. I want them all to redirect to domain.com. Ideally I don't want to have to put the correct domain name in the .htaccess file, because then I'd have to modify each site's .htaccess file seperately. Perhaps this is the only way, as the .htacess file would have no way of knowing the correct domain name- is that true? I thought of adding a short php snippet at the top of each page to redirect to the correct domain name (a short config file is prepended to each html file anyway), but that's probably not great practice.

Any ideas?

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

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

发布评论

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

评论(2

我不吻晚风 2024-10-28 13:12:19

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

IMO alias domains should get their own VirtualHost which just contains a Redirect statement.
The VirtualHosts need to contain the domain names anyway unless you use IP-based VHosts.

柏拉图鍀咏恒 2024-10-28 13:12:19

如果您希望所有域重定向到同一域,例如 domain.com,只需 将其添加到您的 .htaccess 中:

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

If you want all domains to redirect to the same domain, say domain.com, just add this to your .htaccess:

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