Search code examples
.htaccessmod-rewriteurl-rewriting

Rewrite friendly URL, differents patterns


I have the folowing rewrite rule

<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>

Which is, obviously rewriting this

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

to this

mydomain.com/comunity-events.php?myvariables

Now, I want a new redirect which doesn't have a starting "folder", like this

mydomain.com/business-category/business-name

to

mydomain.com/business-profile.php?variables

Is that even possible, given the current configuration, without making a redirect for each category name?


Solution

  • You don't even need filesMatch directives as you can match starting part in RewriteRule itself.

    You can replace your .htaccess with these rules:

    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]