使用 .htaccess 路由 php 查询 [英] Routing php query using .htaccess

查看:24
本文介绍了使用 .htaccess 路由 php 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找更好的解决方案来解决该问题.我没有使用任何框架,如 silex、Yii 等.因此我没有这些框架提供的通用路由处理机制.我的网址格式就像

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

我想要的结果网址是

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

我对.htaccess有一些非常基本的了解,以下是我目前的基础

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

这些代码可以很好地将任何 .php 放到/something 中.但是,我坚持要继续生成获取/routing/create/routing/list/1 的通用解决方案.感谢您的帮助.

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.

推荐答案

让你的 .htaccess 像这样:

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]

这篇关于使用 .htaccess 路由 php 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆