htaccess的重定向规则的子域重定向、规则、htaccess

2023-09-02 01:02:14 作者:怀里的折耳猫

我的网站有很多的子域(第3和第4级),例如:

My site has a lot of subdomains(3rd and 4th levels), for example:

http://de.site.com/catalog
http://product.de.site.com

和我需要automaticaly重定向所有域名以www的,例如:

and I need to automaticaly redirect all domains with "www", for example

http://www.de.site.com/catalog
http://www.product.de.site.com

要URL,而不www的:

to url without "www":

http://de.site.com/catalog
http://product.de.site.com

能否请你帮我这种情况的.htaccess规则?请注意,这个规则必须只用于域3 +级别的应用。

Could you please help me with .htaccess rules for this case ? Please note that this rule must be applied only for 3rd+ levels of domains.

推荐答案

将这个code在 DOCUMENT_ROOT /的.htaccess 文件:

Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]