简体   繁体   English

在htaccess文件codeigniter中重写网址

[英]rewrite url in htaccess file codeigniter

I am using codeigniter for development. 我正在使用codeigniter进行开发。 I've used .htaccess file to shorten my URL ie to remove index.php from all URL's. 我已经使用.htaccess文件来缩短我的URL,即从所有URL中删除index.php。 which is : 这是:

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

I have page in which I'm displaying user's portfolio through following link 我有一个页面,其中通过以下链接显示用户的投资组合

www.example.com/users/username/id www.example.com/users/username/id

Here 'users' is controller, 'username' is first parameter which is not unique and 'id' is second parameter which is unique so that i can display unique portfolio of user. 在这里,“用户”是控制器,“用户名”是不唯一的第一个参数,“ id”是不唯一的第二个参数,因此我可以显示唯一的用户组合。 It calls 'users' index function in which I pass this parameter. 它调用“用户”索引函数,我在其中传递了此参数。

Now, I want my URL to be like this 'www.example.com/username/id' by rewriting URL through .htaccess file. 现在,我希望通过.htaccess文件重写URL,从而使URL像这样的'www.example.com/username/id'。 When I try to do this, codeigniter considers 'username' as controller instead of parameter. 当我尝试执行此操作时,codeigniter会将“用户名”视为控制器而不是参数。

Is there any way to achieve this? 有什么办法可以做到这一点? If yes I'll appreciate your time. 如果是的话,我会很感激您的时间。

Thank you. 谢谢。

From the docs it says you can just add a route so you can hide the users part: http://ellislab.com/codeigniter/user-guide/general/routing.html 从文档中说,您只需添加一条路线即可隐藏用户部分: http : //ellislab.com/codeigniter/user-guide/general/routing.html

$route['username/:num'] = "users/username/$1";

Now you can use the link to show the user (with specific id) like this: 现在,您可以使用链接向用户显示(具有特定ID),如下所示:

<a href="username/$id"> 

Instead of <a href="users/username/$id"> 代替<a href="users/username/$id">

for example 例如

Hopefully it helps you :) 希望它可以帮助您:)

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

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