【发布时间】:2012-11-08 21:52:44
【问题描述】:
我的 url 中都包含 /main/,因为它是我的主控制器。 我想摆脱它。
我在用户指南中了解到,路由类只会重新路由我的 URL,而不是隐藏其中的一部分(根据他们的文档,它是第 1 段)。 因此,我发现了这个:
$route['(:any)'] = "auth/$1";
但是我有404错误(这是我对routing.php所做的唯一修改)
我相信我必须在 .htaccess 中执行此操作,但我不知道应该使用什么语法。目前,我的 .htaccess 是这个:
<IfModule mod_rewrite.c>
SetEnv MAGIC_QUOTES 0
SetEnv PHP_VER 5
# For security reasons, Option followsymlinks cannot be overridden.
# Options +FollowSymlinks -MultiViews
Options +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
DirectoryIndex index.php
#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]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
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
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>
我正在使用几个控制器,我的链接类似于这个:
谢谢
【问题讨论】:
-
如果您不同时发布 htaccess 和 routes.php 并准确告诉我们您想要什么,那将无法提供帮助。路由首先通过 htaccess,然后通过 routes.php 设置,因此您需要先检查 htaccess。通常你只需要让 htaccess 从你的 uri 中删除 index.php。同样在您的示例中,您将所有控制器调用路由到您的身份验证控制器,但您还说您希望它到主控制器?
-
我在 htaccess 中看不到任何会导致意外 404 的内容。因此,您应该能够使用 routes.php 实现您想要的。你能在更具体一点之后解释一下你是什么以及你是如何构建你的 uri 和控制器的吗?根据我的经验,您使用 CI 的次数越多,您需要的路由就越少,因为您可以使用有意的结构来构建您的控制器。 (URI:Controller/Function/Parameter(s))那么您只需要让您的默认控制器(在您的情况下为 main)具有索引功能显示站点主页)
-
谢谢,实际上我想从那种 url : website.com/Controller/Function/Parameter(s) 到 website.com/Function/Parameter(s) 至少对于我的主要控制器(对于另一个,例如博客,管理员......没关系,我想保留它)。所以如果我不应该在 .htaccess 文件中做任何事情,我应该在我的 routes.php 中做什么?我试过 "$route['(:any)'] = "auth/$1";"但是这样做时,单击我的链接时出现 404 错误..
标签: codeigniter url-rewriting routing