Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重定向到.htaccess中的正确域名_Php_Apache_.htaccess_Url Rewriting - Fatal编程技术网

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

Php 重定向到.htaccess中的正确域名,php,apache,.htaccess,url-rewriting,Php,Apache,.htaccess,Url Rewriting,我喜欢Paul Irish的HTML5样板。我已经将.htaccess文件中的大部分内容合并到我的文件中 我喜欢它重定向到域的非www版本的方式,以及在缺少时添加尾随斜杠: # ---------------------------------------------------------------------- # Suppress or force the "www." at the beginning of URLs # --------------------------------

我喜欢Paul Irish的HTML5样板。我已经将.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>
# ----------------------------------------------------------------------
#----------------------------------------------------------------------
#抑制或强制URL开头的“www.”
# ----------------------------------------------------------------------
#相同的内容不应该在两个不同的URL下可用-尤其是不使用和
#一开始没有“www”,因为这会导致SEO问题(重复内容)。
#这就是为什么您应该选择其中一个备选方案,并重定向另一个。
#默认情况下,选项1(无“www.”)被激活。记住:URL越短越性感。
#no-www.org/faq.php?q=class_b
# ----------------------------------------------------------------------
#备选案文1:
#重写“www.domain.com->domain.com”
重写cond%{HTTPS}=在…上
重写cond%{HTTP_HOST}^www\.(.+)$[NC]
重写cond%{HTTP_HOST}^m\.(.+)$[NC]
重写规则^(.*)$http://%1/$1[R=301,L]
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
#向(非文件)URL添加/删除尾部斜杠
# ----------------------------------------------------------------------
#谷歌分别对待带有和不带有斜杠的URL。
#强制使用尾随斜杠通常是首选,但所有这些都非常有用
#重要的是一个正确地重定向到另一个。
# 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 尾斜线问题
# ----------------------------------------------------------------------
#重写“domain.com/foo->domain.com/foo/”
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_URI}!(\[a-zA-Z0-9]{1,5}|/|#(.*))$
重写规则^(.*)$/$1/[R=301,L]
# ----------------------------------------------------------------------
但是,如果有几个停驻的域(或别名),会发生什么?例如,如果主域是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文件前面都有一个简短的配置文件),但这可能不是很好的做法


有什么想法吗?

IMO别名域应该有自己的VirtualHost,它只包含一个
重定向
语句。
虚拟主机无论如何都需要包含域名,除非您使用基于IP的虚拟主机。

如果您希望所有域重定向到同一个域,例如
domain.com
,只需:


我不太确定我会怎么设置这个。我正在使用WHM/cPanel。这是否需要为每个域设置一个单独的帐户?那会有点痛苦。也许我是误解了,因为我对VirtualHost一无所知——“请原谅我的无知!这很可能就是我正在寻找的答案——所以我选择了这个作为答案。不幸的是,我不太明白这在实践中是如何起作用的。虚拟主机对我来说是新的。如果有人能多解释一点,那就太酷了!谢谢,我的旧.htaccess文件中也有类似的内容。然而,正如我在问题中提到的,我试图避免这种重定向代码,因为这意味着我必须将实际域名添加到每个.htaccess文件中。我正在为我创建的每个网站寻找“开箱即用”的.htaccess文件,而html5样板文件.htaccess文件正是这样做的。@baritoneuk显然没有其他方法。要么编辑服务器的
vhosts.conf
并将所有域作为别名添加到主域(正如ThiefMaster建议的那样),要么将我提供给您的代码片段复制/粘贴到每个域的
.htaccess
文件中。没有“开箱即用”的方式让域知道重定向到哪里。感谢您的澄清:-)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.com [NC]
RewriteRule ^(.*)$ http://domain.com%{REQUEST_URI} [R=301,L]