.htaccess 从 URL 中删除 WWW +目录
这对很多人来说似乎不是问题(阅读:我找不到答案),但我想更新以下 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/ 与根目录一样。
This seems to be a non-issue for many people (read: I can't find an answer), but I would like to update the following htaccess code to not only remove the 'www' from the URL, but also any sub-directories that are accessed.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
With this, http://www.example.com/any/ resolves fine, but I want it to redirect to http://example.com/any/ as with the root.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的问题(无法从指向附加域上子目录的 URL 中剥离“www”),但经过一番尝试和错误后,这似乎对我有用:
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:
非 www 的重定向代码 => www 和相反的 www =>非www。 .htaccess 文件中没有硬编码域和方案。因此原始域名和 http/https 版本将被保留。
APACHE 2.4 及更高版本
非 WWW =>网址:
网址=>非 WWW:
注意:不适用于 %{REQUEST_SCHEME} 不可用的 Apache 2.2。为了与 Apache 2.2 兼容,请使用下面的代码或将 %{REQUEST_SCHEME} 替换为固定的 http/https。
APACHE 2.2 及更高版本
非 WWW => WWW:
...或更短版本...
WWW => NON-WWW:
...不可能有更短的版本,因为 %N 只能从最后一个 RewriteCond 中获得...
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:
WWW => NON-WWW:
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:
... or shorter version ...
WWW => NON-WWW:
... shorter version not possible because %N is available only from last RewriteCond ...
我认为您已经很接近了,但请尝试以下操作:
不确定您对子目录的确切含义,但这遵循您的示例。
I think you're close, but try the following:
Not sure exactly what you mean about sub-directories, but this follows your example.
我用这个代码。如果我的访问者的网址中没有 www,则此条件会添加 www 和 url,否则无需添加 www 和 url(因为他已经有了。:))
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. :) )
您好,代码工作正常,除了它在 url 中传递了 www 并带有一些值和斜杠,它显示了 url 中的参数和值。
<代码>
RewriteEngine 开启
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]
选项-索引
选项+FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http:\/\/%1%{REQUEST_URI} [R=301,QSA,NC,L]
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]