PHP的URL路由像一个子文件夹

时间:2018-11-21 14:20:29

标签: php

例如,我有一些这样的文件

/index.php
/cat.php
/genre.php
/categories/hello.php

我想让某人访问我的网站,必须使用url路由我想要的东西,像这样

http://example.com/ ( for index.php )
http://example.com/cat/ ( for cat.php )
http://example.com/genre/ ( for genre.php )
http://example.com/categories/hello/ ( for categories/hello.php )

这就像在网址中删除“ .php”一样,但是在“ .php”中添加了“ /”,我使用了如下的.htaccess脚本

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

但这只会删除“ .php”而不添加“ /”结尾

1 个答案:

答案 0 :(得分:0)

如下更新您的.htaccess

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]