【问题标题】:htaccess redirect php to html with httpshtaccess 使用 https 将 php 重定向到 html
【发布时间】:2014-05-30 06:12:29
【问题描述】:

我对激活 SSL 的 htacces 重写规则有疑问。

在 htacces 中,我将所有地址 .php 重定向到 .html

RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php  
RewriteRule ^(.*)\.php $1.html [R=301,L]  
RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html $1.php [L]

如果我通过以下方式添加 ssl 重定向到 https:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

将 php 重写为 html 不起作用。

例如:

http://domain.com/test.html --- 工作

http://domain.com/test.php --> 重定向到 HTML 和工作

https://domain.com/test.php --> 未重定向 -- 工作

https://domain.com/test.html --> 错误 404 - 页面未找到

已解决 https站点的apache2配置有问题:

AllowOverride None 更改为 AllowOverride All

【问题讨论】:

    标签: php html .htaccess redirect ssl


    【解决方案1】:

    试试这个:

    RewriteBase /
    
    RewriteCond %{HTTPS} !=on
    RewriteCond %{THE_REQUEST} (.*)\.php  
    RewriteRule ^(.*)\.php https://%{HTTP_HOST}/$1.html [R=301,L]
    
    RewriteCond %{HTTPS} !=on
    RewriteCond %{THE_REQUEST} (.*)\.html  
    RewriteRule ^(.*)\.html https://%{HTTP_HOST}/$1.php [L]
    

    【讨论】:

    • 感谢您的帮助.. 问题已解决 -> 原始主题中的解决方案
    【解决方案2】:

    有这样的规则:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
    
    RewriteCond %{THE_REQUEST} \.php [NC]
    RewriteRule ^(.+?)\.php$ $1.html [R=301,L]  
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+?)\.html $1.php [L]
    

    【讨论】:

    • 感谢您的帮助.. 问题已解决 -> 原始主题中的解决方案
    • 很高兴知道,您能否通过单击我的答案左上角的勾号将答案标记为已接受。
    猜你喜欢
    • 2017-07-13
    • 2019-03-18
    • 2012-10-11
    • 2015-11-10
    • 2018-08-31
    • 2014-10-01
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多