成功重写HTTP但不形成HTTPS

时间:2016-05-19 14:26:17

标签: php apache .htaccess mod-rewrite url-rewriting

我在htaccess中有这个。

<IfModule mod_rewrite.c>
    Options -Indexes
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/.]+)/?$ $1.php [QSA]

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

</IfModule>

如果我打开http://tld/index,它会成功重写为https://tld/index.php

但是,如果我打开https://tld/index不会重写https://tld/index.php,只会向我显示&#34; Not Found&#34;页。

我的重写规则有什么问题?

更新 - 我的apache设置:

<VirtualHost *:80>
    DocumentRoot /var/www/domain
    ServerName  dom.tld
</VirtualHost>


<VirtualHost _default_:443> 
  ServerAdmin webmaster@localhost
    DocumentRoot /var/www/domain
    ServerName dom.tld

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/domain/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
    SSLEngine on
    SSLCertificateFile   /etc/letsencrypt/live/dom.tld/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/dom.tld/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/dom.tld/chain.pem

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-6]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

您需要先检查是否存在.php文件,然后才会为{e 1very}请求添加.php,并在.php规则之前保留重定向规则。

Options -Indexes
RewriteEngine On

# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,NE,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

在测试这些更改之前清除浏览器。