.htaccess 重写友好URL,不同模式

eni9jsuy  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(132)

我有以下重写规则

<filesMatch  "^(member)$">
    Options +FollowSymlinks
    RewriteEngine on

    RewriteRule ^/(.*)/(.*)/(.*)$  /member-profile.php?ID=$2 [L]
</filesMatch>

<filesMatch  "^(community-events)$">
    Options +FollowSymlinks
    RewriteEngine on

    RewriteRule ^/(.*)/(.*)/(.*)/(.*)$  /community-events.php?ID=$3 [L]
</filesMatch>

很明显,

mydomain.com/comunity-events/category/id/name

mydomain.com/comunity-events.php?myvariables

现在,我想要一个新的重定向,它没有一个起始“文件夹”,像这样

mydomain.com/business-category/business-name

mydomain.com/business-profile.php?variables

在当前的配置下,不对每个类别名称进行重定向,这是可能的吗?

cgyqldqp

cgyqldqp1#

您甚至不需要filesMatch指令,因为您可以匹配RewriteRule本身中的起始部分。
你可以用这些规则替换你的.htaccess:

Options +FollowSymlinks -MultiViews
RewriteEngine On

# handle /member/...
RewriteRule ^/?(member)/(.*)/(.*)$ /member-profile.php?ID=$2 [L,QSA,NC]

# handle /community-events/...
RewriteRule ^/?(community-events)/(.*)/(.*)/(.*)$ /community-events.php?ID=$3 [L,QSA,NC]

# handle /business-category/business-name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)/(.*)$ business-profile.php?name=$2 [L,QSA]

相关问题