gpt4 book ai didi

php - htaccess 添加 www 如果不是子域,子域删除 www

转载 作者:搜寻专家 更新时间:2023-10-31 20:35:50 25 4
gpt4 key购买 nike

我正在努力做到这一点 - 可能示例是最好的,所以:

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。我试着写这样的东西,但这并没有从子域地址中删除 www :/另外,如果可能的话,我不想使用域和 TLD 域,而是让它尽可能通用。你能帮我看看问题出在哪里吗?

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

我在根目录下的这个文件:

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

关于php - htaccess 添加 www 如果不是子域,子域删除 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35285074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com