使用htaccess路由友好网址时如何在重写中包含https

时间:2018-09-24 18:05:36

标签: .htaccess mod-rewrite

我已经找到正确的htaccess代码将友好的URL路由到我的index.php文件,但是如果用户输入以下内容,它就不会路由到https:

  

example.com/sub-folder/rout /

注意 :该特定的重写htaccess存在于域的子文件夹中

我的htaccess是:

 Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

<IfModule mod_rewrite.c>
    RewriteEngine On

    # The basic rewrtie rule.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [QSA,E=X_REWRITE:1,L]

    # Add the proper X_REWRITE server variable for rewritten requests.
    RewriteCond %{ENV:REDIRECT_X_REWRITE} .+
    RewriteRule ^index\.php - [QSA,E=X_REWRITE:1,E=!REDIRECT_X_REWRITE,L]

    # 301 redirect urls that start with request.php
    RewriteCond %{REQUEST_METHOD} GET [NC]
    RewriteCond %{REQUEST_URI} ^(.*?)/index\.php(.*)$
    RewriteRule ^index\.php %1%2 [QSA,R,L]
</IfModule>

老实说,我对代码的理解不足,无法弄清楚该怎么做,并认为我会尝试:

RewriteRule ^(.*)$ https://example.com/subfolder/index.php/$1 [QSA,E=X_REWRITE:1,L]

但这不起作用。

如何使重写进入https而不是http?

1 个答案:

答案 0 :(得分:0)

您需要在其他任何重写规则之后添加它

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

在应用其他规则之前,这将首先强制执行https。

因此,您最终的.htaccess代码变为

 Options +FollowSymLinks
RewriteEngine On

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

<IfModule mod_rewrite.c>
    RewriteEngine On

    # The basic rewrtie rule.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [QSA,E=X_REWRITE:1,L]

    # Add the proper X_REWRITE server variable for rewritten requests.
    RewriteCond %{ENV:REDIRECT_X_REWRITE} .+
    RewriteRule ^index\.php - [QSA,E=X_REWRITE:1,E=!REDIRECT_X_REWRITE,L]

    # 301 redirect urls that start with request.php
    RewriteCond %{REQUEST_METHOD} GET [NC]
    RewriteCond %{REQUEST_URI} ^(.*?)/index\.php(.*)$
    RewriteRule ^index\.php %1%2 [QSA,R,L]
</IfModule>