使用 .htaccess 删除尾部斜杠,主页/登陆页面除外 [英] Remove trailing slash using .htaccess except for home / landing page

查看:28
本文介绍了使用 .htaccess 删除尾部斜杠,主页/登陆页面除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功修改了我的 .htaccess 文件以删除大多数页面上的尾部斜杠,但我想知道如何免除我的主页/目录?例如:

I've successfully modified my .htaccess file to remove trailing slashes on most pages but I'm wondering how to exempt my home page/directory? For example:

domain.com/test/成功重定向到 domain.com/test

domain.com/test/ successfully redirects to domain.com/test

但是,当我访问我的域时,它会附加根文档

HOWEVER, when I hit my domain it will append the root document

domain.com/重定向到 domain.com/index.php

domain.com/ redirects to domain.com/index.php

是否有条件可以添加以忽略根 url 尾部斜杠,以便它不会尝试删除尾部斜杠并添加我的默认脚本?这是我到目前为止所拥有的:

Is there a condition that I can add to ignore root url trailing slash so that it doesn't attempt to remove the trailing slash and add my default script? Here's what I have so far:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$  /$1 [R=301,L]

推荐答案

好的.经过一系列的反复试验,我回答了我自己的问题.

OK. After a bunch of trial and error I answered my own question.

第三行表示 URI 中必须有内容才能执行重定向,因此如果 url 仅包含初始斜杠,则不会重定向.

The third line denotes that there has to be something in the URI in order to perform the redirect thus not redirecting if the url just contains the initial slash.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.domain.com/$1 [R=301,L]

这篇关于使用 .htaccess 删除尾部斜杠,主页/登陆页面除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆