【问题标题】:Properly Configuring Mod Rewrite while using CodeIgniter使用 CodeIgniter 时正确配置 Mod Rewrite
【发布时间】:2013-11-14 21:08:09
【问题描述】:

我发现了几个关于 CodeIgniter 和 mod-rewrite 问题的相关问题,但是,我还没有找到解决我目前遇到的问题的方法。

我目前有:mywebsiteurl.netfirms.com/mywebsite/index.php/admin/login

我想要实现的是从 URL 中删除 index.php,如下所示:mywebsiteurl.netfirms.com/mywebsite/admin/login

然而,在尝试了几个我在 StackOverflow 上找到的修复后,我最终偶然发现了一个写成 here 的不错的小指南。

我已将我的 Mod-Rewrite 修改为如下所示:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

### Canonicalize codeigniter URLs

# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(home(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
# RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
# RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]

# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

###

# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
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]

# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php

即使在实施此 mod-rewrite 之后,如果我尝试访问 mywebsiteurl.netfirms.com/mywebsite/admin/login,我也会收到 404 错误。如果我去 mywebsiteurl.netfirms.com/mywebsite/index.php/admin/login 我可以加载页面就好了。

我的问题归结为 如何正确配置 Code Igniter 的 mod 重写? 也许我缺少一些设置?我对 mod-rewrite 不够熟悉,无法真正知道哪里出了问题。

我还应该提到我已经在我的 Code Igniter 配置文件中进行了更改。

$config['index_page'] = '';

有人知道我在这里可能做错了什么吗?

提前致谢。

【问题讨论】:

  • 你的 htaccess 文件在哪里?
  • 我的 htaccess 文件位于 mywebsiteurl.netfirms.com/mywebsite/ 访问的 public_html/mywebsite 目录中

标签: php .htaccess codeigniter mod-rewrite


【解决方案1】:

我的 htaccess 文件位于 mywebsiteurl.netfirms.com/mywebsite/ 访问的 public_html/mywebsite 目录中

您需要更新重写基础和另一条规则:

RewriteBase /

需要:

RewriteBase /mywebsite/

还有这一行

RewriteRule ^(home(/index)?|index(\.php)?)/?$ / [L,R=301]

需要

RewriteRule ^(home(/index)?|index(\.php)?)/?$ /mywebsite/ [L,R=301]

这需要删除前导斜杠:

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

所以它就像:

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

当规则的目标中没有前导斜杠时,它是一个相对 URI 路径,并使用基数来确定根。由于您的内容是在mywebsite,因此基础需要反映这一点。

【讨论】:

  • 这似乎已经解决了这个问题。非常感谢。
【解决方案2】:

我想我刚刚为您的问题找到了更好的解决方案,而且非常简单。 在 CodeIgniter 的配置文件中,将 index_page 和 base_url 留空:

$config['base_url']   = '';
$config['index_page'] = '';

CodeIgniter 会自己计算你的基本路径,并且不会在 uri 中使用 index.php 段。

然后在您的 .htaccess 文件中,应用指令将除文件夹和文件名之外的所有 url 重定向到您的 index.php :

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

这将完成这项工作,当您更改项目根文件夹时,您无需更改任何配置文件中的任何内容。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 2012-09-05
    相关资源
    最近更新 更多