2015-05-15 39 views
1

我們很少有開發人員在本地Apache服務器上工作。.htaccess獲取uri的基礎部分

我們設置了一些關於我們本地開發環境結構的基本規則,因爲我們希望有一個適用於所有人的.htaccess文件。

的結構非常簡單:

本地主機/〜用戶名/ API/api_request_request

我試圖創建一個RewriteRole將在自定義變量存儲/〜用戶名/一部分,因爲它變化從一個開發人員到另一個。

這是我走到這一步:

#Set the base in case of local development 
    RewriteCond %{HTTP_HOST} ^(.*)localhost(.*)$ 
    #Here only the /~username should be captured 
    RewriteRule ^(.*)$ $1 [E=LOCAL_BASE:$1] 

    #Set the base in case of local development 
    RewriteCond %{HTTP_HOST} !^(.*)localhost(.*)$ 
    RewriteRule ^(.*)$ $1 [E=LOCAL_BASE:production.server.com] 

    RewriteRule ^(.*)$ %{HTTP_HOST}/%LOCAL_BASE/api/index.php?request=$1 [QSA,NC] 

的問題是,在第一次的RewriteCond我不知道如何捕獲僅〜用戶名部分

回答

1

你可以用它做較不復雜的邏輯

# LOCAL environment 
RewriteCond %{HTTP_HOST} localhost [NC] 
RewriteRule ^([^/]+)/(.*)$ /$1/api/index.php?request=$2 [L] 

# REMOTE environment 
RewriteCond %{HTTP_HOST} !localhost [NC] 
RewriteRule ^(.*)$ /production.server.com/api/index.php?request=$1 [L] 
+0

您可以請多花費大約1美元和2美元嗎? 另外,生產服務器和我們的開發本地主機的區別在於,在開發中它看起來像localhost /〜username/api/apirequest,生產是production.server.com/apirequest。我想創建一個基本變量,它將在/ apirequest之前包含基礎,並且將包含HTTP_HOST(僅僅因爲我希望它更通用),並且我想知道我可以使您的示例更通用 –

+0

在第一條規則中,「$ 1」 〜用戶名和$ 2'後面是什麼。例如,當你在本地時,如果你去'http:// localhost /〜test/something/random/here',你會有'$ 1'作爲'〜test'和'$ 2'作爲'/ something/random/here',它將在內部重寫爲'/〜test/api/index.php?request = something/random/here'。其實你可以用更通用的方式來定義一個基礎,但是它沒有任何複雜性。如果你真的想要它,我可以給你一個例子 –

+0

lurman謝謝你的細節。我是第一條規則,你是如何定義的,或者你在哪裏設置了哪部分將會到達每個變量?我認爲我失去了什麼 –