2013-07-11 192 views
0

我是相当新的CI和一直在尝试如何产生干净的URL。我之前已经完成了这个任务,而没有使用框架编辑我的.htaccess文件,如下所示。漂亮的URL与CodeIgniter

RewriteCond %{REQUEST_URI} !^/(css|js|img)/ 
RewriteRule ^profile/([^/]*)$ profile.php?id=$1 [L] 

随着CI,我曾尝试以下:

#Get rid of the index.php that's in the URL by default 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 

# Profile page 
RewriteCond %{REQUEST_URI} !^/(css|js|img)/ 
RewriteRule ^profile/([^/]*)$ profile?id=$1 [L] 

我知道,在默认情况下,在URL中的控制器的名称后的值(在这种情况下,个人资料控制器),将在控制器类中调用具有相同名称的函数。但是,如果在控制器之后指定的URL中没有值,默认情况下将调用索引函数。我打算将函数名称留空,以便默认调用索引函数。但是,重写规则不起作用。

任何想法?

+0

只需使用CI的路由来处理配置文件URL结构。 –

回答

1

随着.htaccess你可以像这样

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

# Profile page 
RewriteCond %{REQUEST_URI} !^/(css|js|img)/ 
RewriteRule ^profile/([^/]*)$ profile/index/$1 [L] 

在重写就不得不提到的函数名无论是指数函数或任何其他

你一样可以利用CI路由routes.php

$route['profile/(:any)'] = "profile/index/$1"; 

现在在配置文件的索引功能,你可以得到参数

function index($id) { 
echo $id; 
echo $this->uri->segment(3); 
//Both will result the same 
} 
+0

这很好。但是,当我试图通过使用$ route ['users /(:num)/(:num)'] =“users/index/$ 1/$ 2”添加第二个变量来扩展这一点时;在routes.php文件中,当URL中缺少第二个变量时它不起作用。 $ 2变量是可选的。它不必在那里。但是,如果它在URL中,它应该可以工作。 – Lance

+0

因此,首先用单个参数重写routes.php中的两个路由,然后再写入其他 –

+0

谢谢!工作得很好! – Lance