Search code examples
regexapache.htaccessmod-rewritesynology

mod_rewrite - Change Folder to Filename


I'm searching for a RewriteRule or Condition to make this happen:

www.example.com/a/ to a.php
www.example.com/a/b/ to a,b.php
www.example.com/a/b/c/ to a,b,c.php
...

I had this Rules:

RewriteRule ^(.+)/(.+)/(.+)/$ ./$1,$2,$3.php [L]
RewriteRule ^(.+)/(.+)/$ ./$1,$2.php [L]
RewriteRule ^(.+)/$ ./$1.php [L]

That worked for a,b and a,b,c:

www.example.com/products/room/ to products,room.php
www.example.com/products/room/chair/ to products,room,chair.php

but not for a:

www.example.com/products/ to products.php

Can anyone help me out?

EDIT: I figured out that this is a problem with the synology diskstation apache configuration. I checked my code, and the code of anubhava on another server and it worked fine.


Solution

  • Very strange filenames I must say that.

    Anyway try this code in your DOCUMENT_ROOT/.htaccess file:

    RewriteEngine On
    
    ## If the request is for a valid directory
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f    
    RewriteRule ^([^/]+)/?$ /$1.php [L]
    
    RewriteRule ^([^/]+)/([^/]+)/?$ /$1,$2.php [L]
    
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1,$2,$3.php [L]