HTML向子页面添加路径

时间:2020-07-15 05:21:56

标签: javascript html

如何在网页上添加不带.html的路径?

我的主要站点:www.example.com

关于页面:www.example.com/about

另一页:www.example.com/test/contact

2 个答案:

答案 0 :(得分:0)

在根目录中创建一个名为.htaccess的文件 并添加以下代码:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

但是您需要使用http服务器

答案 1 :(得分:0)

有两种方法可以解决此问题:

(a) URL重写:在根文件夹的.htaccess文件中(如果尚不存在,请创建它)进行以下更改:

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-d    # Check that the URL doesn't point to a directory

RewriteCond %{REQUEST_FILENAME}.html -f    # Check that the URL points to a file with the extension html
RewriteRule ^(.*)$ $1.html [NC, L]    # Finally, make the change with a regex and flags like NC which ensures that this rule should be case-insensitive, and 'L' stops processing any more rules if this one is used.

这里是URL重写的好资源:https://aloneonahill.com/blog/url-rewriting-for-beginners/

(b)在每个文件夹中创建一个index.html文件:这样,URL结束于目录名,并且不显示index.html导致更干净的URL(例如, example.com/about,而不是example.com/index.html。

我不确定第二种方法的问题。它看起来笨拙。

相关问题