mod_rewrite - 仅使用最后一个正斜杠之后的任何内容

时间:2011-10-31 22:45:54

标签: regex apache .htaccess mod-rewrite

所以我已经配置了我的.htaccess文件,这样当我加载网页(http://example.com/about)时,它会透明地将其重定向到http://example.com/about.html,但是,我现在很喜欢它能够做到(http://example.com/about/contact),然后让它透明地重定向到http://example.com/about/contact.html。我想理想情况下它完全忽略了URL的“/ about /”部分,只使用了联系部分。

这就是我现在正在使用的:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

1 个答案:

答案 0 :(得分:2)

使用此:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d

# folder/file => folder/file.html
RewriteRule ^([a-z0-9_\-]+)/([a-z0-9_\-]+)/?$ $2.html [NC,L]

# file => file.html
RewriteRule ^([a-z0-9_\-]+)/?$ $1.html [NC,L]