与的.htaccess和mod_rewrite的静态HTML文件清理网址 [英] Clean URLs for static HTML files with .htaccess and mod_rewrite

查看:222
本文介绍了与的.htaccess和mod_rewrite的静态HTML文件清理网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mod_rewrite的总是令我感到困惑......谁能告诉我,我需要得到以下干净的URL规则?所需的URL在左边,在右边实际的URL。

mod_rewrite always baffles me... can anyone tell me the rules I need to get the following clean URLs? Desired URL on the left, real URL on the right.

/our-work/ => /our-work.html
/our-work/some-project/ => /our-work/some-project.html
/contact/ => /contact.html

正如你所看到的,我想迫使尾随斜线上的所有网址了。

As you can see, I want to force trailing slashes on all URLs too.

谢谢!

推荐答案

试试这个规则:

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]

和添加结尾的斜杠:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]

请务必把那些造成外部重定向(明确使用的研究的标志或暗示)在这些规则面前,只是产生一个内部重定向/重写规则。因此,在这种情况下:

Make sure to put those rules that cause an external redirect (explicitly using R flag or implicitly) in front of those rules that just cause an internal redirect/rewrite. So in this case:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]

这篇关于与的.htaccess和mod_rewrite的静态HTML文件清理网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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