如何从链接中删除“index.php”?

时间:2012-11-02 15:15:35

标签: apache .htaccess codeigniter

通常我会访问这样的页面:http://localhost/codeigniter/index.php/auth/login 但是,我希望能够访问这样的页面:http://localhost/codeigniter/auth/login

我没有关于http.conf.htaccess配置的信息。

我在$config['index_page'] = '';

中设置了codeigniter/application/config/config.php

除了uncommentnig“LoadModule rewrite_module modules/mod_rewrite.so

之外,我的http.conf文件处于默认状态

我检查了许多提议的.htaccess文件,但我无法理解重写规则背后的逻辑。

以下是我最成功的.htaccess内容。它位于/www/CodeIgniter/ (最成功的意思是,这是唯一一个由CodeIgniter而不是服务器创建404错误的人。)

    RewriteOptions Inherit
Options -Indexes
Options +FollowSymLinks
RewriteBase /CodeIgniter/
# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

    # activate URL rewriting
    RewriteEngine on

    # do not rewrite links to the documentation, assets and public files
    RewriteCond $1 !^(images|assets|uploads|captcha)

    # do not rewrite for php files in the document root, robots.txt or the maintenance page
    RewriteCond $1 !^([^\..]+\.php|robots\.txt)

    # but rewrite everything else
    RewriteRule ^(.*)$ index.php/$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.

    ErrorDocument 404 index.php

</IfModule>

我讨厌请求直接解决方案但不幸的是,这是我的最后一招:/ 你能帮我解决一下我的问题吗?

3 个答案:

答案 0 :(得分:1)

这是我的重复答案: Codeigniter issues with paths in localhost (XAMPP)

请在项目文件夹中创建.htaccess文件并写:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 

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

您无需在配置文件中的base_url中定义:

$config['base_url'] = ''; // blank it.

答案 1 :(得分:0)

把它放在你的htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
</IfModule>

确保您的服务器上已启用重写模块

答案 2 :(得分:0)

这是我发现的最简单的一个对我有用: -

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]