我刚开始使用 Codeigniter 使用本地服务器 (MAMP) 开发一个简单的静态网站。最初,我访问主页的本地地址是http://localhost/index.php/home
. 即使是简单的 localhost 也会重定向到我的主页。我想从 URL 中删除“index.php”,因此我复制粘贴了我在网上找到的 .htaccess 代码。代码如下所示:
<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 ^(welcome(/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://localhost/$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]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
最初我复制粘贴代码而根本没有更改它(这对我来说是愚蠢的)所以上面代码中的'localhost'最初是'www.domains.tld'。然后当我在浏览器上运行 localhost 时,它指向 www.domains.tld'。我注意到了错误并将其更改为上面的内容,并且 localhost 仍然指向'www.domains.tld' 我删除了 .htaccess 文件以扭转效果,但它仍然做同样的事情。我还更改了 localhost 的根文件夹,但无论我做什么 localhost 都指向“domains.tld”。当我在浏览器的地址栏上输入 127.0.0.1 时,它会正确定向到我的网站。我花了几个小时阅读这种行为的原因,但未能找到解决方案。
任何帮助将不胜感激。
谢谢,H.