简体   繁体   English

如何用日期重写Codeigniter中的URL

[英]How to rewrite url in Codeigniter with date

I have some issues with codeigniter url rewriting. 我对codeigniter网址重写有一些问题。

The problem is that standart htaccess rewriting is not working here. 问题是标准htaccess重写在这里不起作用。

I want that htaccess rewrite this : 我想要htaccess重写这个:

http://www.example.com/home/results?date=2017-10-02

To

http://www.example.com/home/results/2017-10-02

Thanks for help! 感谢帮助!

This not working : 这不起作用:

RewriteRule ^results/([^/]+)$ results?date=$1 [L]

its simple you have to create route in route.php file no need make rewrite rule. 它很简单,您必须在route.php文件中创建路由,而无需制定重写规则。

$route['home/results/(:any)'] = 'home/results';

here is CI official doc for url routing 这是用于URL路由的CI官方文档

https://www.codeigniter.com/user_guide/general/routing.html https://www.codeigniter.com/user_guide/general/routing.html

you need to add the following rules in .htaccess file 您需要在.htaccess文件中添加以下规则

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Check here how CI URL work 在此处查看CI URL的工作方式

https://www.codeigniter.com/user_guide/general/urls.html https://www.codeigniter.com/user_guide/general/urls.html

In CI URL are array segments, you can fetch segments with uri class by giving segment number start with 0 after domain name, here you want to get date you segment is 2 在CI URL中是数组段,您可以通过在域名后以0开头的段号获取uri类的段,在这里您要获取段为2的日期

$this->uri->segment(2);

Here link for CI uri class you can find more helpful methods also 在此处链接CI uri类,您还可以找到更多有用的方法

https://www.codeigniter.com/user_guide/libraries/uri.html https://www.codeigniter.com/user_guide/libraries/uri.html

You can do this with the following RewriteRule 您可以使用以下RewriteRule执行此操作

RewriteEngine on

RewriteCond %{QUERY_STRING} ^date=([0-9]{4}(\-[0-9]{2}){2})$ [NC]
RewriteRule ^home/results$ /home/results/%1? [L]

The %1 is to pull the first group from the regex in the RewriteCond . %1是从RewriteCond的正则表达式中提取第一组。 The trailing ? 尾随? is used so that the query string isn't appended in the rewrite as well. 用于避免将查询字符串也附加在重写中。

In Apache the first parameter of the RewriteRule directive doesn't include the query string so a RewriteCond to check the %{QUERY_STRING} must be used. 在Apache中, RewriteRule指令的第一个参数不包含查询字符串,因此必须使用RewriteCond来检查%{QUERY_STRING}

Also if you want it to be less specific then you can just edit the regex used to match the query string, it currently is very specific in that date must be the only parameter in the query string and that it must be "(4 numbers)-(2 numbers)-(2 numbers)". 另外,如果您希望它不是那么具体,则可以只编辑用于匹配查询字符串的正则表达式,它当前非常具体,因为该日期必须是查询字符串中的唯一参数,并且它必须是“(4个数字) -(2个数字)-(2个数字)”。 You can see a breakdown of the regex here . 您可以在此处查看正则表达式的细分。

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

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