【问题标题】:Codeigniter : Remove index.php after subdomain in .htaccessCodeigniter:在 .htaccess 中的子域之后删除 index.php
【发布时间】:2017-06-01 07:40:49
【问题描述】:

我当前的网址如下所示:

http://www.example.com/testdesign/index.php/tasks

我需要它重定向到:

http://www.example.com/testdesign/tasks

我的 codeignitor 根 .htaccess 文件包含

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
   RewriteCond %{REQUEST_URI} !/system/.* [NC]
   RewriteRule (.?)index\.php/(.*) /$1$2 [R=301,NE,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

我的 apache 服务器上启用了 mod_rewrite。

在我的配置文件中:

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

当我从 localhost 运行它时,它显示如下:

http://www.example.com/tasks

但我只想删除 index.php 而不是子域。

我知道这可能是一个很小的问题,但我找不到错误。

请你给我适当的建议吗?

我们将不胜感激任何形式的帮助。

提前致谢。

【问题讨论】:

  • 也许你应该读一下:http://stackoverflow.com/a/41364059/6054930.
  • @ShutUpMagda,我已经阅读了这份文件并申请了相同的文件,但没有工作。
  • 我还注意到使用site_url () 而不是base_url () 是导致此问题的原因之一。

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

试试这个:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /testdesign

# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

# Prevents user access to the application folder
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
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

即使您没有配置 uri_protocol 和 base_url,它也可以工作。

【讨论】:

  • 您将 .htaccess 文件放在哪里?
  • 我们将 .htaccess 文件放在根目录中,如下所示:/application /system index.php .htaccess
  • 只将它放在 testdesign 文件夹中。不要更改应用程序和系统文件夹中的 .htaccess 文件。
  • 试试吧.. 它会起作用我在我所有的 codeigniter 项目中都使用了它
【解决方案2】:

试试这样。在您的.htaccess 文件中

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
   RewriteCond %{REQUEST_URI} !/system/.* [NC]
   RewriteRule (.?)index\.php/(.*) /$1$2 [R=301,NE,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)/testdesign$ index.php/$1 [L]
</IfModule>

设置$config['base_url']='http://www.example.com/testdesign/'

然后 运行www.example.com/testdesign/tasks

【讨论】:

  • @hikamt,无法正常工作并获得如下返回网址:www.example.com/tasks
  • 如果你设置了 base_url。在 htaccess RewriteRule ^(.*)$ index.php/$1 [L]
  • 还没有工作,仍然得到类似这样的响应:www.example.com/tasks
  • 我只需要删除 index.php 而不是子域。
【解决方案3】:

您应该尝试使用base_url() 而不是site_url()。看,即使你的重写配置设置正确,如果你使用site_url() index.php 文件将被包括在内。

在文档中查看更多信息:site_urlr()base_url()

您还应该在 config/config.php

中将 index_page 配置留空
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = 'index.php'; 

【讨论】:

  • 在我的配置文件中,它已经像这样空白 index_page :$config['index_page'] = ' ';
【解决方案4】:

将以下代码替换为您的 .htaccess 代码

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-06
    • 2015-03-09
    • 2016-01-16
    • 1970-01-01
    • 2016-05-13
    • 2011-10-09
    • 2012-08-15
    • 2023-03-16
    相关资源
    最近更新 更多