.htaccess Remove WWW from URL + Directories

荒凉一梦 提交于 2019-11-27 06:36:10
phpmonkey

I had the same problem (trouble stripping 'www' from URLs that point to a sub-directory on an add-on domain), but after some trial and error, this seems to work for me:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
mikep

Redirection code for both non-www => www and opposite www => non-www. No hardcoding domains and schemes in .htaccess file. So origin domain and http/https version will be preserved.

APACHE 2.4 AND NEWER

NON-WWW => WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WWW => NON-WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]

Note: not working on Apache 2.2 where %{REQUEST_SCHEME} is not available. For compatibility with Apache 2.2 use code below or replace %{REQUEST_SCHEME} with fixed http/https.


APACHE 2.2 AND NEWER

NON-WWW => WWW:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

... or shorter version ...

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WWW => NON-WWW:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

... shorter version not possible because %N is available only from last RewriteCond ...

I think you're close, but try the following:

# force non-www domain
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

Not sure exactly what you mean about sub-directories, but this follows your example.

locphen.vn

I used it and it worked for me

RewriteCond %{HTTP_HOST} ^www.locphen.vn/$ [NC]
RewriteRule ^(.*)$ http://locphen.vn/$1 [R=301,L]

Example: http://www.locphen.vn/he-thong-loc-nuoc-gieng.html -> http://locphen.vn/he-thong-loc-nuoc-gieng.html

I use this code. If my visitor does not have www in his url then this condition adds www with url, otherwise no need to add www with url ( because he already has. :) )

RewriteEngine On
RewriteCond %{HTTP_HOST}  !^www\.YOUR-SITE\.com$ [NC]
RewriteRule ^(.*) http://www.YOUR-SITE.com/$1 [L,R]

Hello, the code works perfectly, except that it passes with the www in a url with some value and slash at the end it shows the parameter and value in the url.

RewriteEngine On SetOutputFilter DEFLATE AddOutputFilterByType DEFLATE text/html text/plain text/xml RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(!.(\.gif|\.jpg|\.png|\.swf|\.css|\.js|\.txt|\.php|\.htm|\.html)|.+[^/])$ /$1/ [R=301,NC] RewriteRule ^(.[^.*]+)\/$ ?jogar=$1 [NC] Options -Indexes Options +FollowSymLinks RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http:\/\/%1%{REQUEST_URI} [R=301,QSA,NC,L]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!