【问题标题】:Can't remove index.php, custom route not working无法删除 index.php,自定义路由不起作用
【发布时间】:2013-08-26 07:29:14
【问题描述】:

我知道这个问题更适合服务器故障,但不幸的是,我因为质量差的问题而被禁止(我在我提出的 2-3 个问题上被否决。)所以问这些问题的下一个最佳地点是这里。

我有两个与 CodeIgniter 路由相关的问题。

第一个问题是我似乎无法摆脱网址中的index.php。我关注了instructions on how to remove it。我的 WAMP 服务器根目录下的 .htaccess 文件(见下文)中有以下 mod 重写代码(CI 位于根目录,而不是在其自己的文件夹中)。我在httpd.conf 文件LoadModule rewrite_module modules/mod_rewrite.so 中取消了对这一行的注释。我从$config['index_page'] = "index.php"; 中删除了index.php。我重新启动了所有 WAMP 服务。

我的第二个问题是我有一个名为search 的控制器和一个名为index 的方法。我想将生成的 URL 从 http://localhost/index.php/search/index 更改为 http://localhost/search/whatever_im_searching_for。我在 routes.php 文件中尝试了以下自定义路由,但没有成功:$route['search/(.*)'] = "search/$1";

RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/

RewriteBase /

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

我很难理解.htaccess 中的代码以及如何使用 CI 的自定义路由。任何帮助将不胜感激。提前谢谢你。


编辑 1

【问题讨论】:

    标签: php .htaccess codeigniter wampserver


    【解决方案1】:

    像这样编辑你的 htaccess:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
    RewriteCond %{REQUEST_URI} ^LoginTut.*
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
    RewriteCond $1 !^(index\.php|images|table-images|js|robots\.txt|css|captcha)
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L]
    </IfModule>
    <IfModule !mod_rewrite.c>
     ErrorDocument 404 index.php
    </IfModule>
    

    要在网址中包含您的搜索词,您可以查看以下内容:

    https://stackoverflow.com/a/12070284/1379394

    【讨论】:

    • +1 感谢您的回复 Kees。不幸的是,我的网址中仍然有index.php。我将您的代码粘贴到我根目录下的 .htaccess 文件中,并重新启动了所有 WAMP 服务。我觉得这很奇怪!我编辑了我的问题以包含我的 www 目录的屏幕截图。我还能尝试什么?
    • 我在config.php中的uri_protocol设置为$config['uri_protocol'] = 'AUTO';这跟我的问题有关系吗?
    • 我在应用程序目录中有另一个 .htaccess 文件,其中包含 Deny from all
    • uri_protocol 默认设置为AUTO。所以这与它无关。
    【解决方案2】:

    第二个问题:

    $route['search/(:any)'] = "search/index/$1";
    

    【讨论】:

    • +1 感谢您的回复。不幸的是,即使我重新启动了 WAMP 服务,它也没有工作。我不明白为什么这些选项都不起作用。它曾经工作到最近。我觉得 httpd 文件中有一个设置阻止我的问题得到解决。我正在重读httpd.apache.org/docs/2.2/howto/htaccess.html
    【解决方案3】:

    检查 Apache 的默认配置文件。在 WAMP 上它可能在

    <WAMPSERVER_HOME>\bin\apache\Apache2.2.xx\conf
    

    如果它看起来像这样:

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
    

    然后更改两者:

    AllowOverride None
    

    到:

    AllowOverride All
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,我只通过写在我的.htaccess 来解决:

      RewriteEngine on
      
      RewriteCond $1 !^(index\.php|images|robots\.txt)
      
      RewriteRule ^(.*)$ index.php/$1 [L]
      

      它运行良好。

      【讨论】:

        猜你喜欢
        • 2011-08-03
        • 2016-05-10
        • 1970-01-01
        • 2013-11-04
        • 2014-08-12
        • 2013-12-29
        • 2019-01-14
        • 2019-01-05
        • 1970-01-01
        相关资源
        最近更新 更多