添加HTML扩展名的文件只能在一个目录 [英] Add html extension to files in one directory only

查看:123
本文介绍了添加HTML扩展名的文件只能在一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图重写如下链接:

/content/about-me

...这样的:

...to this:

/content/about-me.html

这是必要的东西我工作。我不断收到内部服务器错误此htaccess的规则:

It's necessary for what I'm working on. I keep getting internal server errors with this htaccess rule:

RewriteRule ^content/(.*) /content/$1.html [NC,L]

知不知道我做错了吗?对净例子负荷,但不能完全得到它的权利。

Any idea what I'm doing wrong? Loads of examples on the net, but can't quite get it right.

推荐答案

您重写规则(模式)过于宽泛,会赶上已经重写网址。覆盖已经重写URL会导致无穷的改写循环,Apache的智能检测,并中止这样的请求,500错误。

Your rewrite rule (pattern) is too broad and will catch already rewritten URLs. Overwriting already rewritten URL will lead to infinite rewrite loop, which Apache intelligently detects and aborts such request with 500 error.

重写规则最后[L]标志不工作?

例如:

  • 在原始请求: /你好/粉红小猫
  • 1日(初步)改写: /hello/pink-kitten.html
  • 第二个周期:重写 /hello/pink-kitten.html.html
  • 在第3周期:重写 /hello/pink-kitten.html.html.html
  • ...等
  • Original request: /hello/pink-kitten
  • 1st (initial) rewrite: /hello/pink-kitten.html
  • 2nd cycle: rewritten to /hello/pink-kitten.html.html
  • 3rd cycle: rewritten to /hello/pink-kitten.html.html.html
  • ... and so on

更正确的做法(的几个可能的方法之一) - 将检查是否如HTML文件存在在重写:

More correct approach (one of few possible approaches) -- will check if such html file exists BEFORE rewriting:

# add .html file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^content/(.+)$ /content/$1.html [L]

这篇关于添加HTML扩展名的文件只能在一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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