在.htaccess自动检测域名 [英] Detect the domain name automatically in .htaccess

查看:113
本文介绍了在.htaccess自动检测域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除www和直接到域的非www的版本,一些研究,我发现,这两条线将在下面做工作后:

 的RewriteCond%{HTTP_HOST}!^ domain.com $ [NC]
重写规则^(。*)$ http://domain.com/$1 [L,R = 301]
 

不过,当我上面的两行添加到我目前的.htaccess文件下面,我是否需要改变domain.com位的网站域名?如果是这样,我怎么能够改变它,以便它知道由它自身的域名?所以我不必手动更改。

我发现这个<一href="http://stackoverflow.com/questions/19427256/htaccess-automatically-detect-domain-name">article与MAYBE回答这个问题,但我只是不知道如何实现它,我不是一个真正的专家,但我知道有错误,可以摧毁一切,请大家帮忙。

 的DirectoryIndex index.html的index.php文件
ErrorDocument的四百零四分之四百零四

RewriteEngine叙述上
的RewriteBase /

#删除输入code here.php;使用THE_REQUEST至prevent无限循环
#通过这里思想促进了L型标志,该请求被立即重定向
的RewriteCond%{THE_REQUEST} ^ GET \(。*)\。PHP \ HTTP
重写规则(。*)\。PHP的$ $ 1 [R = 301,L]

#删除索引
#通过这里思想促进了L型标志,该请求被立即重定向
#中的斜杠是下一个请求删除,因此是有效的,并
#不要把它放在那里所有
重写规则(。*)/指数$ $ 1 [R = 301,L]

#删除斜线,如果没有目录
#通过这里思想促进了L型标志,该请求被立即重定向
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{REQUEST_URI} / $
重写规则(。*)/ $ 1 [R = 301,L]

#添加的.php访问文件,但不重定向
#在一些主机的RewriteCond%{} REQUEST_FILENAME -f的.php将是真实的,即使
#没有这样的文件存在。是安全的,添加一个额外的条件
#在字符串中的转义点没点
的RewriteCond%{} REQUEST_FILENAME -f .PHP
的RewriteCond%{REQUEST_URI}!(/ | \ .PHP)$
重写规则(。*)$ 1.PHP [L]
 

解决方案

如果你只想重定向 www.example.com 的例子。 COM ,你可以简单地在 WWW匹配。。如果要重定向每一个子域(这通常不是这样),你都不可能匹配并尝试匹配,如果它有两部分。它通常是容易只是改变了 example.com 来是你自己的域名。在任何情况下,你应该任何其他规则之前添加规则。

#Option 1:只有当它开始与WWW重定向。 的RewriteCond%{HTTP_HOST} ^ WWW \。(。*)$ 重写规则^ HTTP://%1%{REQUEST_URI} [R = 301,L] #Option 2:重定向所有的东西! (包括thingy.sub.domain.example.com到example.com) 的RewriteCond%{HTTP_HOST} ^。* \。([^ \。] + \。[^ \。] +)$ 重写规则^ HTTP://%1%{REQUEST_URI} [R = 301,L]

I want to remove www and direct to the non www version of the domain, after some research I found that these two lines below will do the job:

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

However, When I add the above two lines to my current .htaccess file below, do I need to change the domain.com bit to the website domain? if so, how can I change it so it knows the domain name by it self? so I don't have to change it manually.

I found this article with MAYBE an answer to this question, but I just don't know how to implement it I am not really an expert but I know that one mistake and can destroy everything, please help.

DirectoryIndex index.html index.php 
ErrorDocument 404 /404 

RewriteEngine On 
RewriteBase / 

# remove enter code here.php; use THE_REQUEST to prevent infinite loops 
# By puting the L-flag here, the request gets redirected immediately 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP 
RewriteRule (.*)\.php$ $1 [R=301,L] 

# remove index 
# By puting the L-flag here, the request gets redirected immediately 
# The trailing slash is removed in a next request, so be efficient and 
# dont put it on there at all 
RewriteRule (.*)/index$ $1 [R=301,L] 

# remove slash if not directory 
# By puting the L-flag here, the request gets redirected immediately 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} /$ 
RewriteRule (.*)/ $1 [R=301,L] 

# add .php to access file, but don't redirect 
# On some hosts RewriteCond %{REQUEST_FILENAME}.php -f will be true, even if 
# no such file exists. Be safe and add an extra condition 
# There is no point in escaping a dot in a string 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteCond %{REQUEST_URI} !(/|\.php)$ 
RewriteRule (.*) $1.php [L]

解决方案

If you only want to redirect www.example.com to example.com, you can simply match on www.. If you want to redirect every subdomain (which is usually not the case), you can possibly match and try to match if it has two parts. It is usually easier to just alter the example.com to be your own domain name. In any case, you should add the rule before any other rule.

#Option 1: Only redirect if it begins with www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

#Option 2: Redirect all the things! (including thingy.sub.domain.example.com to example.com)
RewriteCond %{HTTP_HOST} ^.*\.([^\.]+\.[^\.]+)$
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

这篇关于在.htaccess自动检测域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆