更改在Codeigniter中显示给用户的URL

更改在Codeigniter中显示给用户的URL

问题描述:

我不知道这是否是一个奇怪的请求,但是我有一个页面显示用户个人资料。当然,每个用户都有唯一的用户名,这些用户名包含在与他们的姓名相关的链接中。单击时会转到他们的个人资料:

I don't know if this is strange request or not, but I have a page with users profiles displaying. Each user has unique username of course and those are included in links to associated with their names. when clicked it goes to their profile:

profile/profile_guest/username

当用户单击一个配置文件时,它会转到用户的配置文件,并且地址栏中的网址为:

When the user clicks on one of the profiles, it goes to user's profile and url in the address bar is:

http://domain.com/profile/profile_guest/ahlam

我要实现的是配置route.php来处理此问题。因此,一旦单击,它将转到个人资料页面,但URL地址栏中显示的内容是:

What I want to achieve is to configure routes.php to handle this. so once clicked, it goes to profile page but what shows in URL address bar is :

domain.com/ahlam

为此,我尝试了:

$route['someTest'] = "profile/profile_guest/$1";

公平地说它不起作用,仍然显示整个URL。我该怎么做才能解决此问题?

Fair to say that it didn't work and still the whole URL displayed. What can I do to fix this?

谢谢

//site.com/john          = "profile/profile_gest/john"
$route['([a-z0-9\-_]+)'] = "profile/profile_gest/$1";
//where $1 = "john", so "john" respects the pattern set below

但是CI路由会理解任何与该模式匹配的URI,例如 index, post,...是配置文件控制器的路由。

But CI Routing will understand any URI match this pattern like "index", "post", ... is a routing for the profile controller.

为了防止您可以在网址中添加规范,例如:

TO prevent that you can add a specification to the url, for examples:

//site.com/john.html
$route['([a-z0-9\-_]+)\.html'] = "profile/profile_gest/$1";

//site.com/user-john
$route['user\-([a-z0-9\-_]+)'] = "profile/profile_gest/$1";


如果您确定任何用户名都不会与其他页面竞争
,请始终将此规则放在底部,以便CI会在此规则之前检查所有其他路由规则。

if you are sure that any usernames will not contest with an another pages always put this rules at the bottom, in order to that CI will check all other routing rules before this one.

注意:在此之前,您的 htaccess 必须设置为从您的网址中删除index.php。

Note: Before that your htaccess have to be set to remove index.php from your urls.

CI URI路由

正则表达式