开发者

Redirect to correct domain name in .htaccess

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:

# ---------------------------------------------------------开发者_StackOverflow-------------
# 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?


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.


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜