Apache使用https将所有内容重定向到index.php

时间:2016-02-01 15:58:40

标签: php apache .htaccess https bootstrapping

我已经阅读了很多关于这个主题的答案,但没有提到如何结合:

将所有流量重定向到index.php + 将所有http重定向到httpS

以下工作非常适合将所有内容重定向到index.php:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]

在我这样做之前,我可以使用这个强制http到https:

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

我找不到混合两者的方法,因此它会使用相同的条件将所有流量重定向到index.php并强制https。

*编辑*

以防其他人像我一样感到困惑。

当我调用重写的url时,https系统地返回404的原因是因为...我的域配置文件不完整,我的website-ssl.conf缺少 AllowOverride All 指令,这就是为什么在我添加url重写之前一切正常。我的非ssl正确设置为url-rewriting所以这就是为什么我花了一点时间才意识到这在https时无效。

然后我在/etc/apache2website-ssl.conf中添加了必要的

<Directory /var/www/vhosts/exemple.com>
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride All
    Require all granted
</Directory>

这是我的.htaccess

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://exemple.com/$1 [R,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php

我希望这会对某人有所帮助。 谢谢你的帮助

2 个答案:

答案 0 :(得分:3)

只需将https重定向规则保持在其他规则之上:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]

答案 1 :(得分:1)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}/index.php
</IfModule>

因此,在上述情况下,如果用户要转到http链接,则会发生(re-writehttpsforward to index.php)(我试过并且正在工作) 。但如果用户直接转到https,那么您应该在rewriting

中使用简单的index.phpdefault-ssl