Php URL重写访问

Php URL重写访问,php,apache,url-rewriting,Php,Apache,Url Rewriting,我有一个小网站,在那里我写了一些规则 RewriteEngine On RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L] 这会将我的所有链接重定向到example.com/index.php。我希望他们将所有带有一个目录example.com/dir的URL重定向到example.com/index.php链接,以及带有两个目录的URL,example.com/dir1/dir2应该重定向到example.com/inde

我有一个小网站,在那里我写了一些规则

RewriteEngine On
RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L]

这会将我的所有链接重定向到example.com/index.php。我希望他们将所有带有一个目录example.com/dir的URL重定向到example.com/index.php链接,以及带有两个目录的URL,example.com/dir1/dir2应该重定向到example.com/index2.php,其余的不应该重定向

您可以使用以下代码。。我会解释一下

customdir=您的子目录

index=index.php

1=您的id

Options +FollowSymLinks
RewriteEngine on
RewriteRule customdir/(\w+)/?(\d+)?/? customdir/$1.php?id=$2

添加两个具有不同正则表达式的重写规则。你需要regex的帮助吗?也许在webmasters.stackexchange.com上更好?一些想法:thx作为你的回复,在我的例子中,URL只假设有目录名,即mysite.com/abc在directory上,或者可能是mysite.com/abc/cde两个目录,我想把目录1作为查询字符串变量1,把目录2作为查询字符串变量2,如果你能告诉我目录名是随机的,因为目录是在运行时创建的,这对我来说是主要的困惑