2014-10-18 58 views
0

REST API首先我实现我的REST API重写规则的Apache2的服务器上,使用下列.htaccess文件:HTTPS与的.htaccess

<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

Now我启用我的服务器上SSL,我想force的API将会使用https so添加以下码至我.htaccess文件中:

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

现在我收到找不到错误的页面。你能看到.htaccess文件中的错误,或者它是否在其他地方?我对这些文件并不熟悉。要创建我的API,我使用了this tutorial

+0

您可以使用https:// domain.com/REST/SomeRule/12 URL直接访问API吗? – anubhava 2014-10-18 12:36:17

+0

没有尝试过这一点。所以也许它不适用于我的api。就像我说的,我跟着我的问题中链接的教程。 – 2014-10-18 15:12:59

+1

如果您使用'https:// domain.com/REST/SomeRule/12'获得404,那么您可能没有正确设置SSL站点。为https站点提供'VirtualHost'配置 – anubhava 2014-10-18 15:16:50

回答

0

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然后莫名其妙地覆盖下一个规则。