Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何重写目录中的任何.htm以将其名称作为参数传递给PHP文件?_Php_Regex_.htaccess_Mod Rewrite_Rewrite - Fatal编程技术网

如何重写目录中的任何.htm以将其名称作为参数传递给PHP文件?

如何重写目录中的任何.htm以将其名称作为参数传递给PHP文件?,php,regex,.htaccess,mod-rewrite,rewrite,Php,Regex,.htaccess,Mod Rewrite,Rewrite,我有很多页面需要静态URL路径,例如: foo/bar/fooey/qwertyuiop.htm 但是,这些页面并不存在。它们都是一样的,只是变量略有不同 因此我创建了/foo/bar/details.php,它接受参数“id”和“type” 有没有办法制定一个重写规则,将/foo及其子目录中的所有.htm重定向到details.php,如下所示: /foo/bar/fooey/abc123.htm --> /foo/bar/details.php?id=abc123 /foo

我有很多页面需要静态URL路径,例如:

foo/bar/fooey/qwertyuiop.htm
但是,这些页面并不存在。它们都是一样的,只是变量略有不同

因此我创建了
/foo/bar/details.php
,它接受参数“id”和“type”

有没有办法制定一个重写规则,将/foo及其子目录中的所有.htm重定向到details.php,如下所示:

/foo/bar/fooey/abc123.htm     --> /foo/bar/details.php?id=abc123

/foo/bar/xyzzy/hellothere.htm --> /foo/bar/details.php?id=hellothere

将被改写为

/foo/bar/details.php?id=check_pie_status&type=peach
换句话说,任何请求的.htm都将被重写到该PHP文件中,而客户端不会看到它。

使用.htaccess

RewriteEngine On
RewriteRule ^(.*)\.html$ details.php?id=$1&%{QUERY_STRING} [L]

将此代码放入
文档\u ROOT/.htaccess
文件:

RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+/[^/]+)/.*?/([^/.]+)\.htm$ $1/details.php?id=$2 [L,QSA,NC]

将其放入
/foo/bar/
文件夹的htaccess文件中:

RewriteEngine On
RewriteRule ^(?:[^/]+/|)([^/.]+)\.htm$ details.php?id=$1 [L,QSA]

这听起来像是一个登录页问题

根据我的经验,我可以提出以下想法:

  • 使用Nginx sites enable文件夹/etc/site enable创建不同的文件夹路径。 这将允许将某个域附加到文件夹路径,或创建将任何/foo发送到certein路径的一般规则

  • 创建为每个域加载特定文件夹的脚本。我认为使用域作为带有路径txt的键的DB是最好的解决方案。。这是一个系统设计问题

  • 在主索引php文件中使用Reg ex调用路径,从$\u服务器URI提取此数据


  • 我有最奇怪的代码问题…可能是@MarcB的重复这是不同的,因为/foo/bar目录(和子目录)中的所有.htm都被重写了,您链接的页面与将每个子目录的目录名传递给新的URL参数有关。我同意这个问题与链接的问题不同。@AustinBurk:您不必担心单次重复投票。需要5票才能将其标记为重复。你的问题不是重复的,所以不会有5票。
    RewriteEngine On
    RewriteRule ^(?:[^/]+/|)([^/.]+)\.htm$ details.php?id=$1 [L,QSA]