繁体   English   中英

CodeIgniter: Is it possible to remove the controller & function from the URL using routes or .htaccess?

[英]CodeIgniter: Is it possible to remove the controller & function from the URL using routes or .htaccess?

我有一个由名为“pages”的表组成的数据库。 我有一个名为“站点”的 controller 和一个名为“页面”的 function ... http://website.com/site/page/page_id

在页面 function 中,它当前查找第 3 个 URI 段 (page_id),然后找到并将其与数据库中的页面行匹配并显示行结果。

我的问题是,是否可以将我的 URL 从http://website.com/site/page/page_id路由/更改为http://website . 然后显然更改我的 function 以找到第一个 URI 段??

我当前的 .htaccess 文件删除了“index.php”,如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /db

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
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
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# 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
</IfModule>

看看这篇文章 在您的应用程序配置中编辑 routes.php 应该可以解决问题。 像这样的东西应该工作:

$route['(:any)'] = 'site/page/$1';

但是,如果您使用其他控制器,则可能需要进行更多工作(我假设“站点”是您的 controller,即 site.php 位于应用程序/控制器中)。

在代码中,如果您的页面 ID 只是数字,您也可以考虑使用:num而不是:any

暂无
暂无

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

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