如何使用301永久重定向将www url重定向到非www url?

时间:2014-07-08 12:17:52

标签: apache .htaccess mod-rewrite redirect

我的网站正在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永久重定向?

我想以此网址格式“https://example.com/”重定向我的域名的所有请求。

2 个答案:

答案 0 :(得分:0)

您可以使用:

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]

答案 1 :(得分:0)

我已经通过编写两个重写规则解决了这个问题。每个vhost中有一个

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

现在它工作正常。

希望这可以帮助某人。