【问题标题】:How to remove .html and add slash in the end of url如何删除 .html 并在 url 末尾添加斜杠
【发布时间】:2019-01-09 06:15:14
【问题描述】:

我一直在尝试删除 .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/ (working)

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

这是我的 .htaccess,我想在 url 末尾添加斜杠。

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]

任何人都可以请帮助我,我将不胜感激。

【问题讨论】:

    标签: apache .htaccess url url-redirection


    【解决方案1】:

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

    删除扩展 .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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2014-09-23
      • 1970-01-01
      • 2021-03-09
      • 2016-12-22
      相关资源
      最近更新 更多