mod_rewrite - 将文件夹更改为文件名

时间:2013-11-13 13:16:37

标签: regex apache .htaccess mod-rewrite synology

我正在寻找RewriteRule或条件来实现这一目标:

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

我有这个规则:

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

适用于a,ba,b,c

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

但不适用于a

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

任何人都可以帮助我吗?

编辑: 我发现这是synology diskstation apache配置的问题。 我检查了我的代码,以及另一台服务器上的anubhava代码,它工作正常。

1 个答案:

答案 0 :(得分:2)

非常奇怪的文件名我必须说。

无论如何,请在DOCUMENT_ROOT/.htaccess文件中尝试此代码:

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]