2014-10-18 51 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然後莫名其妙地覆蓋下一個規則。