重写使用Apache URL重定向根域 [英] Rewrite URL using Apache for redirecting root domain
问题描述
我有这样的场景,我想用下面的场景重定向我的域名。能否请您指点,如果这可以在使用Apache的重写规则来实现?
I have this scenario where I would like to redirect my domains using the following scenario. Can you please advice if this can be achieved using RewriteRule in Apache?
我想任何呼叫重定向使用由 http://www.domainname.com/url 重定向到 http://domainname.com/url 。
I would like to redirect any calls made using http://www.domainname.com/url to redirect to http://domainname.com/url.
我是能够实现上述使用以下重写规则
I was able to achieve the above using the following rewrite rule
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
# END WithoutWWW
如果有人试图访问只需 http://www.domainname.com 或的 http://domainname.com ,我想他们重定向到的 http://dname.com
If anyone tries to visit just http://www.domainname.com or http://domainname.com, I would like them to redirect to http://dname.com
我怎样才能达到使用的Apache重写规则此规则?
我还使用PHP,所以使用PHP的解决方案将是有效的了。
How can I achieve this rule using RewriteRule in Apache? I'm also using PHP, so a solution using PHP would be valid too.
推荐答案
下面是与您的.htaccess结合你现有的和新的code:
Here is your combined .htaccess with your existing and new code:
RewriteEngine on
# redirect domainname.com/ or www.domainname.com/ to dname.com/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com [R=301,L]
# append www to domainname.com
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^ http://domainname.com%{REQUEST_URI} [R=301,L]
这篇关于重写使用Apache URL重定向根域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!