我刚刚开始使用Codeigniter使用本地服务器(MAMP)开发一个简单的静态网站。最初,我访问主页的本地地址是http://localhost/index.php/home
。即使是一个简单的本地主机也会重定向到我的主页。我想从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.htmld"。我删除了.htaccess文件以扭转这种效果,但它仍然做了同样的事情。我还将我的根文件夹更改为localhost,但无论我做什么,localhost都指向"domains.tld"。当我在浏览器的地址栏上键入127.0.0.1时,它会正确地指向我的网站。我花了几个小时来了解这种行为的原因,但没有找到解决方案。
任何帮助都将不胜感激。
谢谢,H.
清除DNS缓存并重新启动浏览器。某些浏览器使用DNS缓存重定向,这样你就可以更快地到达它认为正确的网站。
Firefox对此特别讨厌,所以每次安装时我都会禁用其内部DNS缓存。在about:config
中,创建以下两个整数类型设置(注意:您必须创建这些设置,默认情况下它们不存在):
network.dnsCacheEntries
设置为0
network.dnsCacheExpiration
设置为0
在Chrome中,您需要在隐私设置下关闭DNS预取。
他们这样做的原因是为了让互联网在普通浏览器面前"看起来"更快。对于开发人员来说,这可能是一个相当大的障碍。