.htaccess htaccess动态文件夹和文件名以匹配重写条件

.htaccess htaccess动态文件夹和文件名以匹配重写条件,.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,链接http://localhost/myproject/payment/confirm/1已重定向到http://localhost/myproject/payment/confirm.php?id=1根据此规则: # myproject : root folder # payment : folder myproject/payment # confirm.php : file in myproject/payment/confirm.php RewriteCond %{RE

链接
http://localhost/myproject/payment/confirm/1
已重定向到
http://localhost/myproject/payment/confirm.php?id=1
根据此规则:

# myproject   : root folder
# payment     : folder myproject/payment
# confirm.php : file in myproject/payment/confirm.php 

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} confirm/(.*)$
RewriteRule (.*)/(.*)$ $1\.php?id=$2 [L]
PHP文件可以位于不同的文件夹路径或深度中。例如链接
http://localhost/myproject/client/payment/confirm/1
还可以使用上面的规则重写为
http://localhost/myproject/client/payment/confirm.php?id=1

我正在尝试使其他页面的
confirm
成为动态页面,例如
invoice/2
invoice.php?id=2

我试图在第二次重写中包含
%{REQUEST_FILENAME}
,但没有成功

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} %{REQUEST_FILENAME}/(.*)$
RewriteRule (.*)/(.*)$ $1\.php?id=$2 [L]

如何做到这一点,使我们不应该定义长可能的网页上有id吗?请注意。

此时请求URI和请求文件名的内容彼此非常依赖,因此您试图在此处检查的内容没有多大意义。第二次重写在第一个版本中已经是多余的了——重写规则本身完全能够根据URL路径进行检查。但现在你甚至不想再要那张支票了。当你完全忽略第二个条件时会发生什么?@CBroe是的,我认为我的方向不对。我想用动态文件路径创建一个漂亮的URL(不带.php),并且可能在其尾部有
/id
。所以它可能是
http://localhost/myproject/folder/folder/file/id
http://localhost/myproject/folder/file
http://localhost/myproject/folder/file/id
http://localhost/myproject/folder/folder/file
。然后您必须检查两种不同的可能性—请求的URL附加的
.php
与现有的php文件匹配,或者请求的URL与先删除的
/id
部分匹配,然后附加的
.php
与现有的php文件匹配。@CBroe我定义了所有自定义路径,以确保一致性和灵活性。谢谢你的启发。