htaccess的麻烦隐藏文件扩展名,并迫使斜杠 [英] .htaccess trouble with hiding file extension and forcing trailing slash

查看:139
本文介绍了htaccess的麻烦隐藏文件扩展名,并迫使斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我键入domain.com/page.php它不会自动更改为domain.com/page/。此外,对于在子目录中的网页,如果我跟着一个链接到domain.com/sub/1/~~V一切都很好,但如果我在浏览器中键入它重定向到domain.com/1~~V /

When I type domain.com/page.php it doesn't automatically change to domain.com/page/. Also, for the pages in subdirectories, if I follow a link to domain.com/sub/1/ everything is fine, but if I type it in the browser it redirects to domain.com/1/

这是我在.htaccess

This is what i have in .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

# 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]

任何想法我应该做的子目录和根目录htaccess的文件吗?我在这个新的。

Any ideas what I should do to htaccess files in the subdirectories and root directory? I'm new at this.

推荐答案

更​​改规则的顺序。

试试这个:

RewriteEngine on

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?[^/])$ /$1/ [R=301,L]

# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

这篇关于htaccess的麻烦隐藏文件扩展名,并迫使斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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