带有.htaccess和mod_rewrite的静态HTML文件的语义URL [英] Semantic URLs for static HTML files with .htaccess and mod_rewrite

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

问题描述

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

如您所见,我也想在所有URL上强制使用斜杠.

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]

确保将那些导致外部重定向(明确地使用 R 标志或隐式)的规则放在那些仅引起内部重定向/重写的规则之前.因此,在这种情况下:

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文件的语义URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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