apache伪静态里 http跳转https+去掉index.php

我希望可以htpp访问可以跳转到https上,同时去掉访问网站时出现的index.php
访问http://www.域名.com/index.php/1.html可以直接跳转到 https://www.域名.com/1.html

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>

现在的伪静态是这样的,但是没办法跳转https

求大神指导。

阅读 11.4k
5 个回答

可以这么写:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L] 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

今天做这个也碰到同样的需求,有看到这个链接:https://www.sslshopper.com/ap...
现在我的规则就是上方的,可以使用。

现在我的方法是利用php做https的跳转:

<?php if ($_SERVER["HTTPS"] <> "on")
    {
    $xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

    header("Location: ".$xredir);
    }
?>

然后伪静态就正常写去掉index.php的功能:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>

算是一种折中的做法,可以实现htpp访问可以跳转到https,同时去掉访问网站时出现的index.php

如果有更好的解决方法,不要忘记告诉我,谢谢~

新手上路,请多包涵

跳转和伪静态是两码事
ps:另外请明确跳转的两端,然后设置vhost

伪静态才是rewrite干的事

楼上正解,http可以用301跳转到https,至于index.php用伪静态解决

if ($ssl_protocol = "") {
    return 301 https://$server_name$request_uri; 
} 
if ($host != '你的域名' ) {
    return 301 https://你的域名$request_uri; 
} 

在domain.conf的server段里增加

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题