【问题标题】:Codeigniter htaccess and routes filesCodeigniter htaccess 和路由文件
【发布时间】:2015-08-30 14:52:05
【问题描述】:

我的 htaccess 和路由文件有问题。我想配置我的 default_controller。

$route['default_controller'] = 'fr/index';

我想使用 URL 重写。使用 htaccess 文件删除我的 URL 中的“index.php”。

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/page=$1 [QSA]

RewriteCond $1 !^(index.php|assets/|robots.txt) 

RewriteRule ^(.*)$ index.php/$1 [L]

当一个工作时,另一个不起作用。

你有没有一个想法来配置我的 htaccess 以删除 'index.php' 并将 default_controller 添加到 routes.php ?

我的控制器位于 application/controllers/subfolder/myControllers.php 的子文件夹中

感谢您的回答。

【问题讨论】:

    标签: php apache .htaccess codeigniter mod-rewrite


    【解决方案1】:

    这是我自己使用的 .htaccess 配置。它将删除 index.php 并在域的开头添加 www。

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
    RewriteRule ^ %1/%2 [R=302,L,NE]
    
    SetEnv no-gzip dont-vary
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)/{2,}[?\s] [NC]
    RewriteRule ^ /%1/ [L,R=301]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}  [R=301,L]
    
    RewriteCond %{REQUEST_URI} system|application
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    它也适用于我的子文件夹中的默认控制器。

    【讨论】:

    • 感谢您的回答。第 12 行有问题。什么标志?我在 OVH 有一个共享主机。在 localhost 上,我的配置没有问题,但在共享主机上没有。
    • 你应该在你的服务器上检查你的 mod-rewrite 配置。
    • 好的,谢谢您的回答。您的 htaccess 不适用于我在共享主机上的应用程序。我找到了解决方案
    • 你应该用你找到的解决方案来回答你自己的问题。
    【解决方案2】:

    好的,我找到了解决方案。我不知道为什么,但是这个 htaccess 运行正常:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    我尝试了近 30 种不同的 htaccess。

    【讨论】:

      猜你喜欢
      • 2013-06-19
      • 2017-03-20
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 2011-12-04
      • 2011-06-18
      相关资源
      最近更新 更多