2015-05-15 105 views
1

我創造了我在我的服務器需要一個.htaccess文件:的.htaccess範圍的RewriteCond

<IfModule mod_rewrite.c> 
    #Enable the Rewrite Engine 
    RewriteEngine On 

    #Rewrite the base to/if this is not local host 
    RewriteBase/

    #Set the base in case of local development 
    RewriteCond %{HTTP_HOST} ^(.*)localhost(.*)$ 
    RewriteBase /~asafnevo/api 

    #Redirect every request to the index.php 
    RewriteRule ^(.*)$ api/index.php?request=$1 [QSA,NC] 

    # Prevent direct access to access to the index.php 
    RewriteCond %{THE_REQUEST} index\.php 
    RewriteRule ^(.*)index\.php$ - [F] 


</IfModule> 

我加了:

#Set the base in case of local development 
    RewriteCond %{HTTP_HOST} ^(.*)localhost(.*)$ 
    RewriteBase /~asafnevo/api 

,才能在我的本地主機有不同的基開發環境,但我的問題是RewriteCond何時結束?

我試圖做

RewriteBase /~asafnevo/api [L] 

但是我收到一個500錯誤。

請問有人可以在RewriteCond的範圍內花費範圍嗎?

+0

看看服務器的錯誤日誌瞭解有關500的詳細信息。 –

+0

@anubhava請問您應該花費多少時間來完成它? –

+0

看起來你已經接受了答案,很高興爲你解決了問題。 – anubhava

回答

1

您錯誤地使用了RewriteBase。您不能使用[L]標誌,因爲它不是RewriteRule,因此會出現500錯誤。您的規則中也只能有 RewriteBase。如果文件有多個基地,它將使用最後一個。因此,如果您真的試圖在生產中使用這個產品,那麼它會開始引發問題,產品有多個RewriteBase's。

的RewriteBase指令指定要用於 每目錄(htaccess的)重寫規則指示該替換一個 相對路徑的URL前綴。

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

而且的RewriteCond僅用於規則。

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond

你只得如果你在一個地方盒或保持2個副本,當你需要它的地方只是其重命名爲編輯.htaccess。

3

A RewriteCond僅針對遇到的下一個RewriteRule指令進行評估。 Mod_rewrite將首先評估RewriteRule的第一個參數,然後它會評估它在上一次遇到RewriteRule後遇到的所有RewriteCond,如果全部評估爲true,則它將執行所請求的重寫。

考慮下面的例子:

RewriteCond . . #1 
RewriteCond . . #2 
RewriteRule . . #3 

RewriteCond . . #4 
RewriteRule . . #5 

如果#3場比賽的第一個參數,它會評估條件#1和#2,然後重寫到#3的第二個參數。如果#5的第一個參數匹配,則它將評估#4,然後重寫爲#5的第二個參數。