htaccess从www重定向到非www [英] htaccess redirect from www to non-www
问题描述
我是这个新手.
.htaccess文件中的代码如下:
Code from my .htaccess file goes like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
Redirect 301 /abc/ /abcnew/
我希望它从www重定向到非www,即从 http://www.example.com 到 http://example.com
I want this to redirect from www to non-www i.e., from http://www.example.com to http://example.com
我复制了:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
此处的代码通用htaccess将www重定向到非www .
我还检查了Linux服务器上的/etc/apache2/mods-enabled文件夹.该模块存在"rewrite.load".(我认为这可能意味着服务器上已启用重写功能,但如果我写错了,请更正.)
I also checked in /etc/apache2/mods-enabled folder on my linux server. There "rewrite.load" this module is present.(I think this might mean that rewrite is enabled on my server, but correct me if I am wrong.)
Redirect 301 /abc/ /abcnew/
仅供参考,以上代码可以正常工作(将我的旧链接重定向到新链接).
and just FYI this above code works fine(its redirecting my old links to new links).
我也尝试过这个.
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
对我不起作用.
请帮助.预先感谢...
Please help. Thanks in advance...
此链接我找到了.但是不确定应该编辑什么.有人可以指出吗??
this link I found this. But not sure what should be edited. Can anyone please point out.?
推荐答案
您需要在内部重写规则之前放置外部(完全)重定向
规则,并确保使用 mod_rewrite
仅规则.
You need to place external (full) redirect
rules before internal rewrite ones and also make sure to use mod_rewrite
rules only.
尝试一下:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteRule ^abc/?$ /abcnew/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
这篇关于htaccess从www重定向到非www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!