如何隐藏.html扩展与Apache的mod_rewrite [英] How to hide the .html extension with Apache mod_rewrite

查看:107
本文介绍了如何隐藏.html扩展与Apache的mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一小部分的静态网站,我只是想隐藏.html扩展名:


  • 链接 /富获取静态文件 /foo.html

  • 浏览器仍显示网址 /富

客户端就可以发送了书签的风格 mydomain.com/foo ,而不是 mydomain.com/foo.html

这听起来很简单,我使用了的mod_rewrite 开心地之前(与Word preSS或重定向说),但是这证明更难破解我想。也许我失去了一些东西真的很明显,但我不能在任何地方找到一个解决方案,我一直在这一切的一天!

我们运行我们自己的服务器,所以这个可以去的地方是最好的地方。

附录

下面检查的解决方案,工作得很好。随后一段时间运行后,现场我注意到两个问题:


  1. 所有页面开始出现无样式。我重新加载,清除缓存等,但仍然没有风格。我以前也有这样的烦恼,并不能找到源。


  2. 有一个目录,并命名为画廊的HTML文件,因此/图库链接显示一个目录列表,而不是HTML文件。我应该能够排序的那一个,但进一步的提示欢迎: - )



解决方案

试试这个规则:

 的RewriteCond%{} REQUEST_FILENAME -f的.html
重写规则!* \\。html $进行%{} REQUEST_FILENAME的.html [L]

这将改写,可以追加当被映射到一个现有的文件中的所有请求的的.html

I have a small number of static sites where I simply want to hide the .html extension:

  • the url /foo fetches the static file /foo.html
  • the browser still displays the url /foo

The client can then send out bookmarks in the style mydomain.com/foo rather than mydomain.com/foo.html.

It sounds very simple, and I've used mod_rewrite happily before (say with WordPress or for redirects), but this is proving much harder to crack that I thought. Perhaps I'm missing something really obvious, but I can't find a solution anywhere and I've been at it all day!

We run our own server, so this can go wherever is the best place.

Addendum

The solution checked below worked fine. Then after running the site awhile I noticed two problems:

  1. all pages began to appear unstyled. I reloaded, cleared the cache, etc., but still no-style. I've had this trouble before, and can't locate the source.

  2. There's a directory AND an html file named 'gallery', so the /gallery link shows a directory listing instead of the html file. I should be able to sort that one, but further tips welcome :-)

解决方案

Try this rule:

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

This will rewrite all requests that can be mapped to an existing file when appending a .html.

这篇关于如何隐藏.html扩展与Apache的mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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