1

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

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

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

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

我已将我的 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'] = '';

有人对我在这里可能做错的事情有任何想法吗?

提前致谢。

4

2 回答 2

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 路径,并使用基数来确定根。由于您的内容是 in mywebsite,因此基础需要反映这一点。

于 2013-11-14T22:17:29.490 回答
0

我想我刚刚为您的问题找到了一个更好的解决方案,这很简单。在 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>

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

希望有帮助。

于 2014-10-14T09:59:18.497 回答