我想写一个重写规则来执行以下操作:如果服务器上的特定目录中有以下任何文件作为静态html文件存在,那么我希望.htaccess
为该静态文件提供服务。否则,我希望id(斜线后的第一个数字)作为查询参数传递给www.gallery.com/index.php
www.gallery.com/1-driving-to-the-beach.html
www.gallery.com/2-swimming.html
示例:假设www.gallery.com/2-swimming.html
在服务器上不作为html文件存在。我希望id=2发送到/index.php
,在那里我可以使用
<?php
ob_start();
?>
<html></html>
<?php
file_put_contents('2-swimming.html', ob_get_contents());
?>
问题:
- 我的
.htaccess
文件应该包含什么重写规则 - 当.htaccess重定向url时,如何获取
index.php
中的id
将此规则添加到文档根目录中的htaccess文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENANE} !-f
RewriteCond %{REQUEST_FILENANE} !-d
RewriteRule ^([0-9]+)-(.*)'.html$ /index.php?id=$1 [L]
它将请求的id部分作为参数id发送到index.php
。