繁体   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