htaccess如果不是子域,请添加www;如果子域不删除www,请添加www [英] htaccess add www if not subdomain, if subdomain remove www

查看:103
本文介绍了htaccess如果不是子域,请添加www;如果子域不删除www,请添加www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做到这一点-可能是最好的例子,所以:

I'm trying to make this - probably example would be the best, so:

domain.com -> www.domain.com
www.domain.com -> www.domain.com
subdomain.domain.com -> subdomain.domain.com
www.subdomain.domain.com -> subdomain.domain.com

因此,如果访问者位于子域中,请不要添加www,并且在设置了www.cn后将其删除.否则,如果不在子域中,请添加www. 我试图写这样的东西,但这不会从子域地址中删除www:/此外,如果可能的话,我不想使用域和TLD域,但要使其尽可能通用. 您能帮我解决问题吗?

So if visitor is in subdomain, don't add www and when is set, remove It. Else if not in subdomain, add www. I tried to write something like this, but this doesn't remove www from subdomain adress :/ Also, if possible, I don't want to use domain and TLD domains but make it as much universal as possible. Could you help me where might be the problem please?

# add www if not subdomain
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]


# remove www if subdomain
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.%{HTTP_HOST}+$ [NC]
RewriteRule (.*) http://%1.%{HTTP_HOST}/$1 [R=301,L]

我在根目录中有此文件:

This file I have in root:

# htaccess rules for subdomains and aliases
# to create new subdomain, create a folder www/subdom/(subdomain name)
# to create web for alias, create a folder www/domains/(whole domain name)

# htaccess pravidla pro subdomeny a samostatne weby aliasu
# pro vytvoreni subdomeny vytvorte adresar www/subdom/(nazev subdomeny)
# pro vytvoreni webu pro alias vytvorte adresar www/domains/(cely domenovy nazev)
# dalsi info a priklady: http://kb.wedos.com/r/32/webhosting-htaccess.html

RewriteEngine On

# cele domeny (aliasy)
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]

# subdomeny (s nebo bez www na zacatku)
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

# aliasy - spravne presmerovani pri chybejicim /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

# subdomeny - spravne presmerovani pri chybejicim /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

推荐答案

像这样:

# add www if not subdomain
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# remove www if subdomain
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

这篇关于htaccess如果不是子域,请添加www;如果子域不删除www,请添加www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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