如何删除.html并在网址末尾添加斜杠

时间:2019-01-09 06:15:15

标签: apache .htaccess url url-redirection

我一直在尝试删除.html扩展名,并在URL栏的末尾添加斜杠。

我有如下特定实例

因此,如果有人输入:

example.com-转到-example.com /

example.com/index.html-转到-example.com /

example.com/index-转到-example.com /

example.com/first-page.html-转到-example.com/main-page/ 但作为example.com/main-page

example.com/parent-page/index.html-转到-example.com/parent-page/ (工作中)

example.com/parent-page/child-page.html/-转到-example.com/parent-page/child-page/ ,但作为example.com/parent-page/child -页面

这是我的.htaccess文件,我想在网址末尾添加斜杠。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html 

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

任何人都可以帮助我,我将非常感谢。

1 个答案:

答案 0 :(得分:-1)

如果要从HTML文件中删除.html扩展名(例如,将yoursite.com/wallpaper.html更改为yoursite.com/wallpaper),只需更改上面代码中的最后一行以匹配文件名< / p>

删除扩展名.html

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

在末尾添加斜杠

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

有关更多参考,请点击here