使用 .htaccess 删除尾部斜杠,主页/登陆页面除外

编程入门 行业动态 更新时间:2024-10-15 00:19:18
本文介绍了使用 .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/test/成功重定向到 domain/test

domain/test/ successfully redirects to domain/test

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

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

domain/重定向到 domain/index.php

domain/ redirects to domain/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 ^(.+)/$ www.domain/$1 [R=301,L]

更多推荐

使用 .htaccess 删除尾部斜杠,主页/登陆页面除外

本文发布于:2023-11-01 13:14:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   尾部   页面   主页   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!