【问题标题】:Codeigniter: Rewrite to remove index.php not workingCodeigniter:重写以删除 index.php 不起作用
【发布时间】:2013-01-22 20:39:21
【问题描述】:

我刚刚从本地服务器 (XAMPP) 上传了我的网站。它在本地工作,但由于某种原因,将 index.php 添加到我的 SEF URL 的重写在我的公共服务器中不起作用。这就是我现在所拥有的:

# Avoid listing directory
Options -Indexes

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on

# manage language segment 
RewriteRule ^(es|en)/(.*) $2?lang=$1 [L]

# code that allows to get rid of index.php from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

</IfModule>

这些网址的作用:

www.example.com

www.example.com/index.php/aboutme

虽然这样的 URL 会产生 500 错误

www.example.com/aboutme

这是我试图解决 index.php 删除问题的另一种条件和规则组合:

RewriteCond $1 !^(index.php|css|img|scripts|ckeditor|robots.txt|sitemap.xml) 
RewriteRule ^(.*)$ index.php/$1 [L]

但它会为任何没有 index.php 的 URL 生成 500 错误,包括根 URL www.example.com

你能帮我解决这个问题吗?

【问题讨论】:

    标签: php apache .htaccess codeigniter mod-rewrite


    【解决方案1】:

    您的 RewriteRule 中似乎有一个错字。

    试试这个:

    RewriteCond $1 !^(index.php|css|img|scripts|ckeditor|robots.txt|sitemap.xml) 
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    它适用于我们的 Code Igniter。与第二个示例相比,唯一的区别是 index.php 前面的斜线。

    【讨论】:

    • 没错,但那是来自the user manual
    • 有效!非常感谢!没有那个斜线怎么在我的本地服务器上工作?
    • PHP 在本地环境与托管服务器中运行方式(或者更确切地说,在何处)的差异可能会导致问题。没有更多信息,很难确定。
    • @Brendan:我的本地站点位于 c:/xampp/htdocs/mysite (它不在文档根目录中),而 php.ini 在 c:/xampp/php 中,而在我的远程服务器 Web 文件位于文档根目录中,我不知道 php 位于何处,因为我没有专用服务器。这件事有什么不同吗?在这种情况下,按照您的建议并始终使用 RewriteBase /,我会解决这个问题吗?
    • RewriteBase / 只是告诉RewriteRule 相对路径的位置。例如,RewriteBase /somedir/ 就像说 RewriteRule /somedir/index.php/$1 [L](不使用 RewriteBase)另外,“/”指的是您的文档根目录。
    猜你喜欢
    • 1970-01-01
    • 2014-08-12
    • 2013-12-29
    • 2019-01-14
    • 1970-01-01
    • 2018-07-12
    • 2015-11-20
    • 2020-02-10
    • 2021-04-11
    相关资源
    最近更新 更多