任何人都可以解释一下这个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]  
有帮助吗?

解决方案

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

第二部分中的$ 1由第一部分中的组替换。

这是Symfony规则吗?我的想法是将整个查询字符串作为参数传递给index.php(前端控制器),以便前端控制器可以解析和路由它。

其他提示

浏览器向服务器发送请求(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就像前端控制器一样:它将所有请求路由到应用程序中的正确位置。

如果网址不是以index.php或images或css或js或robots.txt开头,则为字符串“/index.php/"是前缀。

由于index.php可能是一个可执行的php应用程序,因此index.php可以从其cgi环境中读取其余的URL。 (它存储在$ {PATH_INFO})

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top