【问题标题】:redirecting to codeigniter controller using htaccess使用 htaccess 重定向到 codeigniter 控制器
【发布时间】:2012-01-05 08:14:13
【问题描述】:

这是网址

http://rapidsurfing.net/visio/xip8yT

我的网站是用 codeigniter 开发的。因此,当链接上面的 url 时,我想转到一个控制器功能。我怎样才能使用 htaccess 做到这一点。

这是我的 httaccces 代码

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^p\/(.*)$ page.php?q=$1 [L]
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?admin/index/url=$1 [QSA,L]
</IfModule>

url 的值为 xip8yTadmin 是控制器名称,index 是该控制器中的函数。

但是当我将 url http://rapidsurfing.net/visio/xip8yT 粘贴到地址栏中时,它会加载 404 not found 错误。 我的代码有什么问题或错误? 我的默认控制器是管理员。

【问题讨论】:

    标签: php .htaccess redirect codeigniter-2


    【解决方案1】:

    首先确保您在routes.php 文件中设置了默认控制器。您的问题的完整答案是我使用 CI 的方式。 CodeIginter 本身建议使用.htaccess 将所有请求重定向到您的索引页面和默认控制器,这就是您所做的。但我建议使用这段代码:

    <IfModule mod_rewrite.c>
    
        RewriteEngine On
        RewriteBase /yourbasefoldername
    
        ### Canonicalize codeigniter URLs
    
        # If your default controller is something other than
        # "welcome" you should probably change this
        RewriteRule ^(yourdefaultcontrollername(/index)?|index(\.php)?)/?$ / [L,R=301]
        RewriteRule ^(.*)/index/?$ $1 [L,R=301]
    
        # Removes trailing slashes (prevents SEO duplicate content issues)
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.+)/$ $1 [L,R=301]
    
        # Enforce www
        # If you have subdomains, you can add them to 
        # the list using the "|" (OR) regex operator
        # RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
        # RewriteRule ^(.*)$ http://localhost/dfgamez/$1 [L,R=301]
    
        # Enforce NO www
        # RewriteCond %{HTTP_HOST} ^www [NC]
        # RewriteRule ^(.*)$ http://localhost/dfgamez/$1 [L,R=301]
    
        ###
    
        # 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
        RewriteRule ^(.*)$ index.php/$1 [L]
    
    </IfModule>
    
    <IfModule !mod_rewrite.c>
    
        # Without mod_rewrite, route 404's to the front controller
        ErrorDocument 404 /index.php
    
    </IfModule>
    

    您只需将站点地址后的每个斜杠添加到您的index 函数参数即可。

    和平!

    【讨论】:

    • 我还将我的默认控制器设置为管理员
    • 是在你的本地主机还是主机上?确保您的应用程序位于主主机上,像这样删除 RewriteBase:RewriteBase /,没有任何名称。如果您编写了错误的代码或未正确配置 CI 的一部分,则必须发生该错误。顺便说一句,将config.php 文件中的$config['base_url'] 设置为空。
    • 不,它在主主机中。在主主机中有一个目录visio,它是我网站的根文件夹。错误仍然显示
    • 所以基本 URL 应该是RewriteBase /visio。我不知道到底是什么问题。我的意见是写一个简单的应用程序,专注于这个问题,并为自己做一个尝试和错误,以找出你的地址的实现方式。顺便说一句,我推荐 CI 论坛,它有充分知情的用户。 codeigniter.com/forums
    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2018-09-25
    相关资源
    最近更新 更多