.htaccess 重写规则不起作用(CodeIgniter)

.htaccess 重写规则不起作用(CodeIgniter)

以下是我想要做的事情:我使用的是 CodeIgniter,因此所有 URL 都采用 的形式http://example.com/index.php/controller/function。但是,我编写的脚本本质上是一个带有一些内置统计功能的美化链接缩短器,因此我希望所有 URL 都采用 的形式http://example.com/link。但是,我还希望脚本的管理面板可在 处使用http://example.com/admin/。我改编了 CodeIgniter“从 URL 中删除 index.php”示例 .htaccess 以适应我的情况,它在我的本地主机(EasyPHP 5.3.9)上正常工作,但是当我将其上传到生产服务器(HostGator)时,所有对管理面板的请求都像常规链接一样被处理并传递给链接控制器(index.php/redirection/link)而不是管理控制器(index.php/admin)。这是我的 .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 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 the administration panel,
    # and if so sends the request to the admin controller
    RewriteRule ^admin/?$ index.php/admin/ [L]
    RewriteRule ^admin/([^.]+)/?$ index.php/admin/$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 the redirect controller
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/redirection/link/$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>

我做错了什么吗?任何帮助或建议都将不胜感激!

答案1

删除以下几行.htaccess

RewriteRule ^admin/?$ index.php/admin/ [L]
RewriteRule ^admin/([^.]+)/?$ index.php/admin/$1 [L]

添加这两行路线.php

$route['admin']="<directory_name_in_contollers>/<controller_name>";
$route["admin/(:any)"]="<directory_name_in_contollers>/$1";

例子:

$route['admin']="admin/welcome";
$route["admin/(:any)"]="admin/$1";

welcome.php是一个控制器位于application\controllers\admin\welcome.php

答案2

在命令“RewriteEngine On”下输入“重写库 /“ 或者 ”RewriteBase /应用程序名称/“如果您的应用程序位于子目录“app_name”中。

希望有所帮助。

阿尔弗拉迪克

相关内容