htaccess RewriteRule 解决多种格式的URL解析

V1版本的RewriteRule 解析式:

RewriteRule ([a-zA-Z]{1,})/([a-zA-Z]{1,})/([0-9]{1,}).html$ 

能解决格式如:

http://localhost/xxx/ooo/111.html

但是,当需要格式如http://localhost/xxx时,会出问题,如:
输出链接为http://localhost/test时,出现异常形如下:

Not Found
The requested URL /test was not found on this server.

Apache/2.4.27 (Win64) PHP/5.6.31 Server at localhost Port 80

也就是说,无法支持多种URL写法。经过尝试,V2版本.htaccess内容如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index.php\.php|images|robots\.txt)
RewriteRule ([a-zA-Z]{1,})$ /index.php?root=$1
RewriteRule ([a-zA-Z]{1,})/([a-zA-Z]{1,})$ /index.php?root=$1&branch=$2
RewriteRule ([a-zA-Z]{1,})/([a-zA-Z]{1,})/([0-9]{1,}).html$ /index.php?root=$1&branch=$2&leaf=$3

这样,即可支持多种格式的URL。
示例.htaccess支持的格式形如:

http://localhost/xxx/ooo/111.html
http://localhost/xxx/ooo
http://localhost/xxx

至此,达到目的

你可能感兴趣的:(运维)