htaccess - 在 URL 末尾添加尾部斜杠

问题描述 投票:0回答:1

目前,文件扩展名

.php
已从我的 URL 中正确删除。但
www.example.com/thema
下的 URL 不带尾部斜杠,
www.example.com/thema/
下的尾部斜杠不带尾部斜杠。

我的

.htaccess
文件内容:

DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{HTTP_HOST} !^www\.example\.com$
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

    # redirect file.php to /file/
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
    RewriteRule ^ /%1/ [R=302,NE,L]

    # added .php extension to /file by checking presence of file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

您能给我一些建议,需要更改以下几点:

  • 域名应在 https 下可用
  • 主页应仅在
    www.example.com
    下可用(无尾部斜杠)
  • 子页面只能使用尾部斜杠(例如
    www.example.com/thema/
.htaccess mod-rewrite trailing-slash
1个回答
0
投票
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

在此规则中您应该重定向到

https
,而不是
http

然后,您可以在此之后添加另一个规则,附加尾部斜杠:

# Append trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+[^/]$ /$0/ [R=301,L]
© www.soinside.com 2019 - 2024. All rights reserved.