与的.htaccess和mod_rewrite的静态HTML文件清理网址静态、网址、文件、htaccess

由网友(染名利为你)分享简介:mod_rewrite的总是令我感到困惑......谁能告诉我,我需要得到以下干净的URL规则?所需的URL在左边,在右边实际的URL。mod_rewrite always baffles me... can anyone tell me the rules I need to get the following...

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]
阅读全文

相关推荐

最新文章