1

我想编写一个重写规则来执行以下操作:如果服务器上存在以下任何文件作为特定目录中的静态 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());
?>

问题:

  1. 我的.htaccess文件应该包含什么重写规则?
  2. 当 url 被 .htaccess 重定向时,如何获取id ?index.php
4

1 回答 1

2

将此规则添加到文档根目录中的 htaccess 文件中:

RewriteEngine On
RewriteCond %{REQUEST_FILENANE} !-f
RewriteCond %{REQUEST_FILENANE} !-d
RewriteRule ^([0-9]+)-(.*)\.html$ /index.php?id=$1 [L]

这会将请求的 id 部分index.php作为参数id发送。

于 2013-08-28T20:41:04.630 回答