Search code examples
.htaccessdirectory

htaccess redirect to subfolder and file


I think there are many htaccess redirect samples but I can't find a case similar to mine.

I have many sub folder my root

/suppliers
 |- /suppliers/abc
 |- /suppliers/xyz

/merchants
 |- /merchants/abc
 |- /merchants/xzy

etc.

I want to redirect them when I type

/m/yyy -> /merchants/yyy/index.php
/m/yyy/abc -> /m/merchants/yyy/abc.php
/m/yyy/abc/ -> /m/merchants/yyy/abc/index.php

Can anyone advise me on how I can do that?


Solution

  • You may use these rules in site root .htaccess:

    RewriteEngine On
    
    # /m/yyy rule
    RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]
    
    # /m/yyy/abc rule
    RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]
    
    # /m/yyy/abc/ rule
    RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]