如何在1和2目录级别添加不同的重写规则?

时间:2015-01-20 11:18:17

标签: regex .htaccess mod-rewrite url-rewriting

如何强制尾部斜杠仅限于 1 目录级别:

 http://example.com/dir1 -> http://example.com/dir1/

和 强制 .html 后缀为 2 目录级别

  http://example.com/dir1/dir2 or http://example.com/dir1/dir2/ -> 

  -> http://example.com/dir1/dir2.html

我试过这些规则,但它们似乎没有正常工作:

 RewriteCond %{HTTP_HOST}/([a-zA-Z0-9_-]+)$
 RewriteRule %{HTTP_HOST}/([a-zA-Z0-9_-]+)/$ [L]
 RewriteCond %{HTTP_HOST}/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$
 RewriteRule {HTTP_HOST}/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ [L]

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这些:

RewriteEngine On
RewriteBase /
# Rule number 1
RewriteRule ^([^/]+?)(\.html?)?/?$ /$1/ [L]
# Rule number 2
RewriteRule ^([^/]+/[^/]+)/?$ /$1.html [L]

如果您希望重写的网址显示在浏览器中,请将标记更改为[R,L]