简体   繁体   English

CodeIgniter Mod重写规则和控制器

[英]CodeIgniter Mod Rewrite Rules and the Controller

Learning PHP, I am playing around with mod_rewrite and CodeIgniter. 学习PHP,我在玩mod_rewrite和CodeIgniter。 I have configured my .htaccess file correctly with 我已经正确配置了.htaccess文件

RewriteEngine On
RewriteRule ^(resources)/(.*) $1/$2 [L]
RewriteRule ^(user_guide)/(.*) $1/$2 [L]
RewriteRule (.*) index.php?$1 [L]

I understand a bit of regex, and can appreciate what happens here. 我了解一些正则表达式,可以欣赏这里发生的事情。 The rewrite rules are applied and the server than handles the final URL which in the above case- attaches index.php (the front controller) to the "pretty" URL. 应用重写规则,然后服务器处理最终的URL,在上述情况下,将index.php(前端控制器)附加到“漂亮的” URL。 So far so good. 到现在为止还挺好。

I now want a URL pattern : 我现在想要一个URL模式:

/<person-name>/at/<place>

to get translated to : 转换为:

/index.php/person/list?personName=$1&place=$2

And i handle the request at my list function in the person controller. 我在人控制器的列表函数中处理请求。 I do not understand why the following doesn't work: 我不明白以下原因为何不起作用:

RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/list?personName=$1&place=$2 [L]

What am i doing wrong/where is my understanding flawed? 我做错了/我的理解哪里有缺陷? I see that the placeholders are extracted correctly ($1 and $3), however, it throws a CodeIgniter 404. 我看到占位符被正确提取($ 1和$ 3),但是,它抛出了CodeIgniter 404。

Many thanks! 非常感谢!

It's possible that the simplest fix will fix your issue. 最简单的修复程序可能会解决您的问题。 By wrapping "at" in parentheses, you're creating another matching group, which means that $2 will always be "at." 通过将“ at”括在括号中,您将创建另一个匹配组,这意味着$ 2将始终为“ at”。 That could be breaking everything. 那可能会破坏一切。 You want index.php?person/list?personName=$1&place=$3 But you may have noticed that issue and fixed it without fixing the problem, in which case, read on. 您想要index.php?person/list?personName=$1&place=$3但您可能已经注意到了该问题,并在未解决问题的情况下对其进行了修复,在这种情况下,请继续阅读。

Check out How to make CodeIgniter accept "query string" URLs? 看看如何使CodeIgniter接受“查询字符串” URL? . It seems to indicate that you can't mix and match the segment-based approach and the query string approach. 似乎表明您不能混合使用基于段的方法和查询字符串的方法。 Without seeing your controller code, I can't say for certain, but I'd start investigating there. 在没有看到您的控制器代码的情况下,我不能确定地说什么,但是我将在那儿开始调查。 You might also try: 您也可以尝试:

RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php?person/list/$1/$3 [L]

which would do the same thing the general-purpose CI redirect rule below does; 下面的通用CI重定向规则将执行相同的操作; send the URL off to index.php as a query string for processing. 将网址作为查询字符串发送给index.php进行处理。 You've said you got it working with routes, so rather than passing a query string to your controller, you can expect person and place as two arguments. 您已经说过,它可以与路线配合使用,因此可以将person和place作为两个参数,而不是将查询字符串传递给控制器​​。 CI's handling of query strings leaves a lot to be desired, and I've never tried to MOD_REWRITE something including a query string into a query string. CI对查询字符串的处理还有很多不足之处,而且我从未尝试过将包含查询字符串的内容MOD_REWRITE转换为查询字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM