简体   繁体   English

如何使用.htaccess将尾随“ /”添加到网站

[英]How to add trailing “/” to website with .htaccess

Hi i'm trying to add a trailing "/" to my website for the past few weeks and every code and website tutorial to edit the .htaccess i've tried has failed to work. 嗨,我试图在过去的几周内在我的网站上添加尾随的“ /”,并且我尝试的每个代码和网站教程都无法编辑.htaccess。 All my webpages end with ".html". 我所有的网页均以“ .html”结尾。 The first part of my htaccess code forces the use of "https" and "www". 我的htaccess代码的第一部分强制使用“ https”和“ www”。 What I want is that + when a user clicks on a link on my site to say "a href="help.html" it will load help.html but it will appear in the address bar as " https://www.example.com/help/ " I don't care if I need to change all my links to omit the ".html" or if I need to change all my filenames to omit the ".html" extension. I just want that trailing "/" + my www and https force. Here is my code so far: 我想要的是+当用户单击我网站上的链接说“ a href =“ help.html”时,它将加载help.html,但在地址栏中显示为“ https://www.example .com / help / “我不在乎是否需要更改所有链接以省略” .html“,还是需要更改所有文件名以省略” .html“扩展名。 /“ +我的www和https强制。这是到目前为止的代码:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=302]

Options +MultiViews
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=302,L]

PS I am aware of the 302 redirect as i'm testing, once my code is working i'll change to 301. Any help would be appreciated. PS:我在测试时就知道302重定向,一旦我的代码正常工作,我将更改为301。我们将不胜感激。

Add this and If the URL doesnt have a trailing slash the htaccess will redirect the browser to the URL with a the slash. 添加此内容,如果URL不包含斜杠,则htaccess将使用斜杠将浏览器重定向到URL。

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM