当我输入地址栏时,我网站的第一页称为 index.html:
http://websit/index.html
它有效
但
现在我的老板问我他想要这个名字 http://websit/index
我怎样才能做到这一点?
标准的 Web 服务器行为是将“index.html”文件加载为路径调用的根文件。这意味着:
http://websit/
与此相同:
http://websit/index.html
这样做(如下)只会导致更多问题,因为索引似乎是路径而不是文件:
http://websit/index
因此,如果您想清理任何东西,只需提及http://websit/index.html
只是http://websit/
如果您使用的是 Apache,则需要检查是否启用了 Mod Rewrite,然后阅读有关 .htaccess 和 mod rewrite 的文章。
解决方案:在根目录(index.html 所在的位置)上创建一个名称为 .htaccess 的文件。然后,复制此代码(将其粘贴到 .htaccess 文件中)并使用没有 .html 的 website/index 再次测试它
#comments
RewriteEngine On
#base directory for this file, if its root, then its one /, if its test then its /test
RewriteBase /
#if the URL for the resource that requested is not exist as file
RewriteCond %{SCRIPT_FILENAME} !-f
#if the URL for the resource not exist as directory
RewriteCond %{SCRIPT_FILENAME} !-d
#then it anything types after the website/() redirect it to ().html
RewriteRule ^(.*)$ $1.html [NC,L]
#the ^ and $ used for matching whole string (from beginning to end)
#the () used for grouping matching strings
#the . used for matching any character
#the $1 represents the match group, for our example we used one () then its $1
[1] http://httpd.apache.org/docs/current/howto/htaccess.html