301 redirect from www to non

     
I would lượt thích to redirect www.example.com khổng lồ example.com. The following htaccess code makes this happen:

RewriteCond %HTTP_HOST ^www.example.com RewriteRule ^(.*)$ http://example.com/$1 But, is there a way to lớn bởi this in a generic fashion without hardcoding the domain name name?


*

*

RewriteEngine OnRewriteBase /RewriteCond %HTTP_HOST ^www.(.*)$ RewriteRule ^(.*)$ https://%1/$1 Same as Michael"s except this one works :P


*

But if we need lớn vày this for separate http & https:

RewriteCond %HTTPS offRewriteCond %HTTP_HOST ^www.(.*)$ RewriteRule ^(.*)$ http://%1/$1 RewriteCond %HTTPS onRewriteCond %HTTP_HOST ^www.(.*)$ RewriteRule ^(.*)$ https://%1/$1

*

*

Redirect non-www khổng lồ www (both: http + https)

RewriteCond %HTTPS offRewriteCond %HTTP_HOST !^www.(.*)$ RewriteRule ^(.*)$ http://www.%HTTP_HOST/$1 RewriteCond %HTTPS onRewriteCond %HTTP_HOST !^www.(.*)$ RewriteRule ^(.*)$ https://www.%HTTP_HOST/$1
If you want khổng lồ bởi this in the httpd.conf tệp tin, you can bởi it without mod_rewrite (and apparently it"s better for performance).

Bạn đang xem: 301 redirect from www to non

ServerName www.example.com Redirect 301 / http://example.com/I got that answer here: https://serverfault.com/questions/120488/redirect-url-within-apache-virtualhost/120507#120507


Here are the rules to redirect a www URL khổng lồ no-www:

########################## redirect www to no-www#########################RewriteCond %HTTP_HOST ^www.(.+) RewriteRule ^(.*) http://%1/$1 Here are the rules to lớn redirect a no-www URL lớn www:

########################## redirect no-www lớn www#########################RewriteCond %HTTP_HOST ^(?!www.)(.+) RewriteRule ^(.*) http://www.%1/$1 chú ý that I used NE flag lớn prevent apađậy from escaping the query string. Without this flag, apabít will change the requested URL http://www.example.com/?foo%20bar lớn http://www.example.com/?foo%2250bar


RewriteCond %HTTP_HOST ^www.(.*)$ RewriteRule ^/(.*)$ https://%1/$1 The RewriteCond captures everything in the HTTP_HOST variable after the www. & saves it in %1.

The RewriteRule captures the URL without the leading / & saves it in $1.

Xem thêm: Cách Kiểm Tra Phiên Bản Office Chính Xác Mà Bạn Đang Sử Dụng


Complete Generic WWW handler, http/https

I didn"t see a complete answer. I use this to handle WWW inclusion.

Generic. Doesn"t require domain name info.Forces WWW on primary domain: www.domain.comRemoves WWW on subdomains: sub.domain.comPreserves HTTP/HTTPS status.Allows individual cookies for domain / sub-domains

Please let me know how this works or if I left a loophole.

RewriteEngine OnRewriteBase /# Force WWW. when no subtên miền in hostRewriteCond %HTTP_HOST ^<^.>+.<^.>+$ RewriteCond %HTTPSs ^on(s)|off RewriteRule ^ http%1://www.%HTTP_HOST%REQUEST_URI # Remove WWW. when subdomain(s) in host RewriteCond %HTTP_HOST ^www. RewriteCond %HTTPSs ^on(s)|off RewriteCond http%1://%HTTP_HOST ^(https?://)(www.)(.+.)(.+.)(.+)$ RewriteRule ^ %1%3%4%5%REQUEST_URI

Chuyên mục: Domain Hosting