首页 文章

.htaccess从URL目录中删除WWW

提问于
浏览
41

这对许多人来说似乎不是问题(读:我找不到答案),但我想更新以下htaccess代码,不仅要从URL中删除'www',还要删除任何子代码访问的目录 .

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

有了这个,http://www.example.com/any/解析得很好,但是我希望它像根一样重定向到http://example.com/any/ .

6 回答

  • 1

    我有同样的问题(从指向附加域上的子目录的URL中剥离'www'的问题),但经过一些试验和错误后,这似乎对我有用:

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

    非www => www和对面www =>非www的重定向代码 . .htaccess文件中没有硬编码域和方案 . 因此将保留原始域和http / https版本 .

    APACHE 2.4和NEWER

    非WWW => WWW:

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

    WWW =>非WWW:

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

    注意:不在Apache 2.2上工作,其中%不可用 . 为了与Apache 2.2兼容,请使用下面的代码或将%替换为固定的http / https .


    APACHE 2.2和NEWER

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

    ...或更短的版本......

    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 =>非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]
    

    ...短版本不可能,因为%N只能从最后一个RewriteCond获得...

  • -1

    我认为你很接近,但请尝试以下方法:

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

    不确定你对子目录的确切含义,但这是你的例子 .

  • -3

    我用它,它对我有用

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

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

  • 14

    我用这个代码 . 如果我的访问者在他的网址中没有www,则此条件会添加带有网址的www,否则无需添加带网址的www(因为他已经拥有 . :))

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

    您好,代码完美无缺,除了它在带有某个值的url中传递www并在结尾处显示斜杠,它在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]

相关问题