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 a stucked to continue to generate a general solution for getting /routing/create
and /routing/list/1
. Appreciate for any helps.
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]
来源:https://stackoverflow.com/questions/20021346/routing-php-query-using-htaccess