Routing php query using .htaccess

2020-02-15 06:10发布

I am trying to find a better solution to solve the issue. I am not using any framework like silex, Yii, etc. Thus I do not have the general routing handling mechanism provided by these frameworks. My URL patterns are like

routing.php
routing.php?action=create
routing.php?action=list&id=1

and the resulting URL I want to have are

/routing
/routing/create
/routing/list/1

I have some very basic understanding of .htaccess, below is my current foundings

RewriteEngine On
RewriteBase /name/

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

These codes work well to put any .php to /something. However, I am stuck to continue to generate a general solution for getting /routing/create and /routing/list/1 . I appreciate for any helps.

1条回答
来,给爷笑一个
2楼-- · 2020-02-15 07:06

Have your .htaccess like this:

RewriteEngine On
RewriteBase /name/

RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ $.php1?action=$2 [L,NC,QSA]

RewriteCond %{DOCUMENT_ROOT}/name/$1.php -f    
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?action=$2&id=$3 [L,NC,QSA]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L]
查看更多
登录 后发表回答