问题
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
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
To do that, I tried :
$route['someTest'] = "profile/profile_guest/$1";
Fair to say that it didn't work and still the whole URL displayed. What can I do to fix this?
Thanks all
回答1:
You should use something like
$route['sometest/(:any)'] = "your/link/$1";
The '$1' is like a parameter you gonna receive from the '(:any)' part.
回答2:
//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
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";
Or
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.
Note: Before that your htaccess have to be set to remove index.php from your urls.
CI URI Routing
Regular expression
来源:https://stackoverflow.com/questions/16152441/changing-url-displayed-to-user-in-codeigniter