【问题标题】:How to redirect a www url to non-www url using 301 permanent redirection?如何使用 301 永久重定向将 www url 重定向到非 www url?
【发布时间】:2014-08-29 04:20:01
【问题描述】:

我的网站在virtualmin 控制面板的虚拟服务器上运行,目前它正在重定向302 临时URL,而我在.htaccess 文件中添加了重定向规则。

这是我的.htaccess文件配置:

Options -Indexes
RewriteEngine On
RewriteBase /

RewriteRule ^index.html$ index.php
RewriteRule ^(([a-zA-Z0-9-_]+)?)\.html$ page.php?pagId=$1
RewriteCond %{http_host} !^example.com$ [nc]
RewriteRule ^(.*)$ https://example.com/$1 [r=301,nc,L]

请让我知道我需要对我的网络服务器进行哪些更改,以便正确重定向 301 永久重定向?

我想以这种 URL 格式“https://example.com/”重定向对我的域的所有请求。

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect


    【解决方案1】:

    你可以使用:

    Options -Indexes
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
    
    RewriteRule ^index\.html$ index.php [L,NC]
    
    RewriteRule ^([\w-]+)\.html$ page.php?pagId=$1 [L,NC,QSA]
    

    【讨论】:

    • 感谢 anubhava 的帮助,但仍然无法正常工作,仍然 302 重定向。
    • 您在浏览器中输入了哪个网址,哪些网址无效?这个 .htaccess 在哪里?
    • 很高兴知道它成功了,您能否通过单击我的答案左上角的勾号将答案标记为已接受。
    【解决方案2】:

    我已经通过编写两条重写规则解决了这个问题。每个虚拟主机一个

    我在 .htaccess 文件中保持相同 >

             Options -Indexes
             RewriteEngine On
             RewriteBase /
             RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
             RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
             RewriteRule ^index\.html$ index.php [L,NC]
             RewriteRule ^([\w-]+)\.html$ page.php?pagId=$1 [L,NC,QSA]
    

    在 /etc/httpd/conf/httpd.conf 文件中 >

            <VirtualHost*:80>
            .
            .
            RewriteEngine on
            RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
            RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
            </VirtualHost>
    
    
             <VirtualHost*:443>
            .
            .
            RewriteEngine on
            RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
            RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
            </VirtualHost>
    

    现在一切正常。

    希望这可以帮助某人。

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 2015-08-03
      • 2019-03-09
      • 2011-11-05
      • 2013-12-20
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多