非 www .htaccess 重定向 - 忽略其他子域

发布于 2024-08-28 16:49:46 字数 621 浏览 0 评论 0原文

我有一个“非 www”的 .htaccess 重定向,如下所示:

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

它正在工作。但是,除了 www 之外,我还有一些子域。 例如,如果我调用 http://shop.example.com ,它会将我重定向到: http://www.shop.example.com

我不想将子域写入 .htaccess 文件,它应该自动工作,只需忽略除 www 和 '' 以外的任何内容,如下所示:

if subdomain =='' -> redirect to www.HTTP_HOST....
elseif subdomain !='' && subdomain !='www' -> do nothing.

谢谢!

I have a .htaccess redirect for "non www" like this:

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

it is working. But, i have also some subdomains other than www.
If I call for example http://shop.example.com it redirects me to:
http://www.shop.example.com

I dont want to write the subdomains into the .htaccess file, it should work automatically, just ignore anything else than www and '' like this:

if subdomain =='' -> redirect to www.HTTP_HOST....
elseif subdomain !='' && subdomain !='www' -> do nothing.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

青朷 2024-09-04 16:49:46

尝试这个规则:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

并且还要考虑 HTTPS:

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

Try this rule:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

And to also take HTTPS into account:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
〗斷ホ乔殘χμё〖 2024-09-04 16:49:46

试试这个:

Options +FollowSymlinks

RewriteEngine On

RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]

RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]

刚刚尝试过 internetagentur.drupal-webmaster.de (子域) - 主要是没有www。

try this:

Options +FollowSymlinks

RewriteEngine On

RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www\.(.*)//((s)on|s.*)$ [NC]

RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]

Just tried it with internetagentur.drupal-webmaster.de (the subdomain) - the main is without www.

染柒℉ 2024-09-04 16:49:46

如果你想要一个 php 解决方案,你可以使用类似的东西:

define('URL', 'yourdomain.com/');

// fix : impose www rule
if (strpos($_SERVER['SERVER_NAME'], 'www') === false ) {
    header('Location: http://www.'.URL.$_SERVER['REQUEST_URI']);
    die();
}

这也会重定向到最初请求的页面。

If you want a php solution you could use something similar to this:

define('URL', 'yourdomain.com/');

// fix : impose www rule
if (strpos($_SERVER['SERVER_NAME'], 'www') === false ) {
    header('Location: http://www.'.URL.$_SERVER['REQUEST_URI']);
    die();
}

This would also redirect to the originally requested page.

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