简体   繁体   English

如何使用.htaccess文件更改Codeigniter的URL

[英]How to change the codeigniter's URL using .htaccess file

I'd like to know how to do the URL rewriting using .htaccess file. 我想知道如何使用.htaccess文件重写URL。 My web application is based on PHP CodeIgniter and I am going to implement the SEF function. 我的Web应用程序基于PHP CodeIgniter,并且我将实现SEF功能。

Current URL is 当前网址为

http://localhost/index.php/sale/list

In this URL, "sale" is a controller name and "list" is a method name. 在此URL中,“ sale”是控制器名称,“ list”是方法名称。 I'd like to replace the URL with http://localhost/index.php/product-sales/list 我想用http://localhost/index.php/product-sales/list替换URL

I would think this can be done easily with .htaccess file if you know .htaccess well. 我认为,如果您非常了解.htaccess,可以使用.htaccess文件轻松完成此操作。

Thanks for your attention and time. 感谢您的关注和时间。 Regards, 问候,

Put this into your .htaccess file and will rewrite everything that's index.php/sale/... to index.php/product-sales/... 将其放入您的.htaccess文件中,并将index.php / sale / ...的所有内容重写为index.php / product-sales /...。

RewriteEngine on

RewriteRule ^index\.php/sale/(.+)$ index.php/product-sales/$1 [R=301,L]

BUT I agree with @Daniel that it might be better to fix it in system rather than in htaccess 但我同意@Daniel的观点,将其修复在系统中而不是htaccess中可能更好

Edit: if i understand it right now, you just want to have a new url for the same controller so after you added the upper rule to your htaccess (so you redirect the old url's to the new one) you still need to add the new routes to the routes file like: 编辑:如果我现在理解,您只想为同一个控制器有一个新的URL,因此在将上限规则添加到htaccess中(以便将旧的URL重定向到新的URL)之后,您仍然需要添加新的URL路由到路由文件,例如:

$route['product-sales/(:any)'] = 'sale/$1';

It should work now, if that's not the desired effect then explain more pls... 它现在应该可以工作,如果那不是理想的效果,那么请解释更多...

All routing is defined in route.php file. 所有路由都在route.php文件中定义。

Navigate to your project. 导航到您的项目。 Open application/config/routes.php file. 打开application / config / routes.php文件。

$route['sale/list'] = 'product-sales/list/';

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

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