Htaccess重写规则仅重定向主网站页面

时间:2019-04-11 08:40:09

标签: .htaccess

我有此规则将所有http请求重定向到https:

 <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule (.*)index\.php$ /$1 [NS,R=301,L]

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

        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]

        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]

        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        RewriteCond %{SERVER_PORT} 80
        RewriteRule ^(.*)$ https://sitename.ro$1 [R,L]
    </IfModule>

规则在主页上运行正常,但在使用单一产品时不起作用。

此外,我尝试添加这些规则:

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我被重定向到https,但我得到404。

是由托管服务提供商或规则引起的吗?

2 个答案:

答案 0 :(得分:0)

请在规则的开头进行更改(https重写上移):

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://sitename.ro%{REQUEST_URI} [L,R=301]

答案 1 :(得分:0)

与我的托管服务提供商联系,经过一些修改后,所有请求都重定向到https://www。 这是htaccess文件:

  #Reinnoire Let's Encrypt
    RewriteEngine On
    RewriteRule ^.well-known - [L]
    #Final Reinnoire Let's Encrypt

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
        <IfModule mod_headers.c>
            <FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css)$">
            Header set Access-Control-Allow-Origin "*"
            </FilesMatch>
        </IfModule>
        RewriteEngine On
        RewriteBase /
        RewriteRule (.*)index\.php$ /$1 [NS,R=301,L]
        #RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
        #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    </IfModule>
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>

我为同一个网站有2个域,对于这两个域,请求都重定向到https。