的.htaccess删除.html文件/目录优先重写 [英] .htaccess remove .html file/directory priority rewrite

查看:120
本文介绍了的.htaccess删除.html文件/目录优先重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写,所以你可以访问文件,而无需html的和不添加斜线。这里的code使用:

I'm trying to rewrite so you can access files without .html and without adding a trailing slash. here's the code being used:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html

但对于我所在的服务器有这个在我的文件结构:

But for my file structure on the server where I have this:

/public_html
          ..
          /beginners
           beginners.html

和内部/新手]文件夹中的其他文件,如:

and inside /beginners folder there's other files, e.g.

          /beginners/page1.html

我需要重写的工作是这样的:

I need rewrites to work like this:

如果用户输入网址: website.com/beginners - 服务器将返回的内容 文件 beginners.html 目前返回目​​录 /初学者的内容。所以我需要服务器首先检查是否 beginners.html 存在,如果是的话 - 那么服务器提供 beginners.html 不目录 /初学者

if user inputs url: website.com/beginners - the server would return the contents of file beginners.html currently it returns the contents of the directory /beginners . so i need the server to first check if beginners.html exists, if yes - then server serves beginners.html not the directory /beginners

如果用户访问网址 website.com/beginners/page1 服务器应该首先检查了,如果 page1.html 存在于文件夹初学者,如果发现该文件 page1.html 则提供的内容文件 page1.html

if the user accesses url website.com/beginners/page1 the server should first check up if page1.html exists in the folder beginners and if it finds the file page1.html then it serves the contents of the file page1.html

如何才能做到这一点?

推荐答案

下面是做它的.htaccess:

Here's the .htaccess that does it:

Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html

这篇关于的.htaccess删除.html文件/目录优先重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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