【问题标题】:HTTPS and REST API with .htaccess带有 .htaccess 的 HTTPS 和 REST API
【发布时间】:2014-12-13 21:14:42
【问题描述】:

首先,我使用以下.htaccess 文件在 apache2 服务器上为我的 REST API 实现了重写规则:

<IfModule mod_rewrite.c>
    RewriteEngine On 

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)$ index.php?rquest=$1 [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*)$ index.php [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^(.*)$ index.php [QSA,NC,L] 
</IfModule>

效果很好。如果我输入例如http://mypage.de/REST/SomeRule/12我的index.php在文件夹REST/收到信息SomeRule/12

现在我在我的服务器上启用了SSL,我想强制 API 使用https,所以我将以下代码添加到我的.htaccess 文件中:

RewriteCond %{SERVER_PORT}   !^443$
RewriteRule  (.*)  https://%{HTTP_HOST}/$1   [L]

现在我收到一个找不到页面的错误。您能在.htaccess 文件中看到错误,还是必须在其他地方?我对这些文件不是很熟悉。为了创建我的 API,我使用了 this tutorial

【问题讨论】:

  • 可以直接使用https://domain.com/REST/SomeRule/12 URL访问API吗?
  • 这个也没试过。所以也许它不适用于我的api。就像我说的,我按照问题中链接的教程进行操作。
  • 如果您也使用https://domain.com/REST/SomeRule/12 得到 404,那么您可能没有正确设置 SSL 站点。为 https 站点提供 VirtualHost 配置
  • 所以对于https://domain.com/REST/SomeRule/12 它说:The requested URL /REST/SomeRule/12 was not found on this server. 似乎链接到 index.php 不起作用。
  • 是的,您可能在 https 站点中使用不同的目录作为 DocumentRoot

标签: .htaccess rest ssl https apache2


【解决方案1】:

anubhava 的帮助下,我找到了解决方案。

当我更新我的服务器以使用https 运行时,我的文件夹sites-enabled 包含一个新文件sites-enabled/default-ssl。在这个文件中我必须设置

AllowOverride All

就像我已经使用http 所做的那样。我为.htaccess 文件添加了正确的解决方案如下:

<IfModule mod_rewrite.c>
    RewriteEngine On 

    #use https
    RewriteCond %{SERVER_PORT}   !^443$
    RewriteRule  (.*)  https://%{HTTP_HOST}/%{REQUEST_URI}  [L]

    #used for api
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)$ index.php?rquest=$1 [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*)$ index.php [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^(.*)$ index.php [QSA,NC,L] 
</IfModule>

我无法使用

RewriteRule  (.*)  https://%{HTTP_HOST}/$1  [L]

因为 $1 然后不知何故被下一条规则覆盖了。

【讨论】:

    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 2019-05-07
    • 2018-11-28
    • 1970-01-01
    • 2015-06-16
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多