.htaccess 从 URL 中删除 WWW +目录

发布于 2024-11-17 18:18:04 字数 381 浏览 4 评论 0原文

这对很多人来说似乎不是问题(阅读:我找不到答案),但我想更新以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

入画浅相思 2024-11-24 18:18:04

我遇到了同样的问题(无法从指向附加域上子目录的 URL 中剥离“www”),但经过一番尝试和错误后,这似乎对我有用:

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

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]
↙温凉少女 2024-11-24 18:18:04

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

APACHE 2.4 及更高版本

非 WWW =>网址:

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

网址=>非 WWW:

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

注意:不适用于 %{REQUEST_SCHEME} 不可用的 Apache 2.2。为了与 Apache 2.2 兼容,请使用下面的代码或将 %{REQUEST_SCHEME} 替换为固定的 http/https。


APACHE 2.2 及更高版本

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

...不可能有更短的版本,因为 %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:

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 ...

魂归处 2024-11-24 18:18:04

我认为您已经很接近了,但请尝试以下操作:

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

不确定您对子目录的确切含义,但这遵循您的示例。

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.

眼泪都笑了 2024-11-24 18:18:04

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

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

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]
没有你我更好 2024-11-24 18:18:04

您好,代码工作正常,除了它在 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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文