簡體   English   中英

解釋這個mod_rewrite規則

[英]Explain this mod_rewrite rule

誰能解釋一下這個mod_rewrite規則在做什么?

我正試圖評論該文件,但代碼似乎與我認為它正在做的相反

# Enable rewriting of URLs
RewriteEngine on


# Allow specified file types to be accessed
# Thing to test = URL
# Condition = not starting with  
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)


# RewriteRule will only be performed if the preceeding RewriteCond is fulfilled
# Remove index.php from all URLs     
# Pattern = anything (0 or more of any character)
# Substitution = index.php + the rest of the URL    
RewriteRule ^(.*)$ /index.php/$1 [L]  

瀏覽器向服務器發送請求(Apache,因為您正在使用mod_rewrite):

獲取個人資料/編輯

Apache接受此請求,並在其配置文件中看到您已將其配置為通過mod_rewrite傳遞所有請求。 因此,它將字符串'profile / edit'發送到mod_rewrite。 然后,Mod_rewrite將您指定的規則應用於它,然后將請求(以我在上一篇文章中解釋的方式)轉換為'index.php / profile / edit'。 mod_rewrite完成后,Apache繼續處理請求,並看到'哦,這家伙正在請求文件index.php'。 所以它調用php解釋器然后解析並執行index.php - 並將'/ profile / edit'作為參數。 php代碼(在您的情況下為CI)解析這些參數並知道如何在應用程序中調用正確的模塊。

所以基本上,這是一種始終調用index.php的方法,即使url沒有指定index.php。 這樣,index.php就像前端控制器一樣:它將所有請求路由到應用程序中的正確位置。

^ = begin of line
( = begin group
.* = any character, any number of times
) = end group

第二部分中的$ 1由第一部分中的組替換。

這是Symfony規則嗎? 我們的想法是將整個查詢字符串作為參數傳遞給index.php(前端控制器),以便前端控制器可以解析和路由它。

如果URL不是以index.php或images或css或js或robots.txt開頭,則字符串“/index.php/”是前綴。

由於index.php可能是一個可執行的php應用程序,因此index.php可以從其cgi環境中讀取其余的URL。 (它存儲在$ {PATH_INFO}中)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM