regex - 解释这个mod_rewrite规则

标签 regex apache mod-rewrite codeigniter

谁能解释一下这个 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 充当前端 Controller :它将所有请求路由到应用程序中的正确位置。

关于regex - 解释这个mod_rewrite规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/179405/

相关文章:

java - 正则表达式查找带条件的子字符串

php - 在生日时向不同客户发送邮件

php - sudo apachectl 开始无法在 OS X Yosemite 上运行

php - 在php中使用正则表达式多次查找字符之间的字符串值

regex - 使用正则表达式将字符串中间的模式与 awk、sed、grep ... something linux-y 匹配

http - htaccess : force http on all pages and https on select directories

apache - 当在 apache httpd 中完成 URL 重写时,POST 请求被转换为 GET

apache - 使用 .htaccess 从子域提供根域内容

python - 如何使用正则表达式检测字符串中的符号?

php - 分页脚本——从两个表中获取数据