我遇到了mod重写的问题。我也是一个初学者。我使用的笨并试图改写如下:Codeigniter和.htaccess mod重写
http://url.dev/news/news_selection?item=59
看起来像这样:
http://url.dev/news/news_selection/59/
我按照一个教程:https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/(更具体地说是:“模式和替换“部分)
问题是我写我的RewriteRule如下,它仍然不会重写页面后去(例如http://url.dev/news/news_selection?item=59)
RewriteRule ^news/news_selection/([0-9]+)/?$ news_selection?item=$1 [NC,L]
任何意见,我哪里去错了?谢谢。
我不确定路由和控制器是否会影响此问题,但我在下面添加了它们供参考。
在我的路线笨我有
//_News Folder
$route['news/(:any)'] = 'pages/view2/_news/$1';
1:_News是文件夹
2:news_selection是php文件
3 :页面是控制器
我控制器具有视图2以下功能:
public function view2($sub ='', $page='')
{
if (! file_exists(APPPATH.'/views/pages/'.$sub.'/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$sub.'/'.$page.'.php', $data);
$this->load->view('templates/footer', $data);
}
我的.htaccess的其余部分:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#When your application folder isn't in the system folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
您能否请详细解释一下?该网站正常工作:http://url.dev/news/news_selection?item=59作为一个网址。我只想清理它在你输入的地方:http://url.dev/news/news_selection/59/并重写它以传递。 – CrazyGrunt
我对此不甚了解。但是我读了一篇关于CodeIgniter URL的文章,如果你设置了'$ config ['enable_query_strings'] = TRUE;'它会显示一个url,就像查询字符串一样。因为第一次使用CI,我的网址是正常的,不像字符串。 您可以阅读更多:[链接](https://www.codeigniter.com/userguide3/general/urls.html) –
感谢您的帮助。这不是我的答案,因为它会与其他一些事情一起帮助破解帮助者。我认为这是通过变量访问模型和视图的不同方式。我终于搞清楚了我是如何做到的,并在下面发布了我的答案。 – CrazyGrunt