重写应用于文件夹的网址规则

时间:2014-12-16 14:23:06

标签: .htaccess url-rewriting

我的网址如下:www.mysite.com/directory1/directory2/directory3。

使用htaccess我想定义一个重写规则,以便能够将此示例中的最后一个url(directory3)重定向到directory3.html

有人有想法吗?

1 个答案:

答案 0 :(得分:0)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /RestAPI/

RewriteCond %{DOCUMENT_ROOT}/RestAPI/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
  1. 此规则捕获整个URI,但后引用$1中的最后一个可选斜杠除外。
  2. 它使用%{DOCUMENT_ROOT}/RestAPI/$1.html构建完整的文件系统路径,其中%{DOCUMENT_ROOT}是内部mod_rewrite变量,表示系统中实际DOCUMENT_ROOT路径的值。
  3. 使用-f检查此.html文件是否存在。
  4. 如果条件为真,则将其路由到.html文件。