url重写以将https重定向到非www

时间:2014-01-04 19:09:57

标签: apache .htaccess mod-rewrite redirect ssl

我刚将网站http改为https。我已经将http重定向到https版本了。我还想将www版本重定向到非www。 到目前为止无法成功。

http://www.domain.com => https://domain.com

https://www.domain.com => https://domain.com

这是.htaccess文件;

<ifModule mod_rewrite.c>
    RewriteEngine On
        redirect 301 /makale /sozluk
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L]

        RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
        RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]

        RewriteCond %{HTTPS} !=on
        # This checks to make sure the connection is not already HTTPS
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>

谢谢。

1 个答案:

答案 0 :(得分:1)

你应该删除这一位:

RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

然后在RewriteEngine On

下添加
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.sporapp\.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]

您需要重新编写重定向才能重写,否则请求会在重定向之前被更改。

另外,你有mod_alias指令会干扰对/index.php的重写:

redirect 301 /makale /sozluk

应该是:

RewriteRule ^makale/(.*)$ /sozluk/$1 [L,R=301]