【问题标题】:Apache Redirect all to index.php with httpsApache 使用 https 将所有内容重定向到 index.php
【发布时间】:2016-05-10 03:47:46
【问题描述】:

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

将所有流量重定向到 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 重写,所以这就是为什么我花了一点时间才意识到这在 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

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

【问题讨论】:

  • 甚至没有查看您的代码,您是否真的尝试将所有代码放在同一个文件中? (保存重复的 RewriteEngineRewriteBase 指令。)
  • 这都在同一个.htaccess 是的,当我尝试两者时我没有重复RewriteEngine

标签: php apache .htaccess https bootstrapping


【解决方案1】:

只需将 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]

【讨论】:

  • 感谢您的回复 Starkeen,这对我不起作用我已经尝试过这个,我希望它也能解决问题。如果我打电话给localhost/login 我会得到预期的结果,如果我运行它 https 它会给出 404正如预期的那样。
  • @arnaudbochot 该规则与 https 无关。它只是将http请求重定向到https。究竟是什么问题?
  • 这是真的@Starkeen,我已经在 apache .conf 文件中挖掘了该站点的安全版本,但它缺少 AllowOverride 规则...
【解决方案2】:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}/index.php
</IfModule>

因此,在上述情况下,如果用户要访问 http 链接,则(re-writehttpsforward to index.php)都会发生(我尝试过并且它工作正常)。但是如果用户直接去https,那么你的default-ssl中应该有简单的rewritingindex.php

【讨论】:

    猜你喜欢
    • 2011-12-30
    • 2019-04-30
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2016-03-04
    • 2012-12-11
    相关资源
    最近更新 更多