您的位置 首页 php

PHP开发常见URL模式,你了解几个?

在使用PHP框架(CI/TP)搭建项目时, 我们经常使用 Nginx / Apache 的URL Rewrite模块实现伪静态。例如, 访问URL: 同时我们也可以通过访:访问,打开的页面是一模一样的。在Nginx设置如下URL Rewrite规则就可以实现:

rewrite ^/(.*)$ /index.php?$1 last;
 

除了伪静态URL外,我们还可以通过其他几种URL模式访问。例如CI框架:

CI框架

CI框架中可选三种URL 模式,在application/config/config.php可以定义:

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string. The default setting of 'REQUEST_URI' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'REQUEST_URI' Uses $_SERVER['REQUEST_URI']
| 'QUERY_STRING' Uses $_SERVER['QUERY_STRING']
| 'PATH_INFO' Uses $_SERVER['PATH_INFO']
|
| WARNING: If you set this to 'PATH_INFO', URIs will always be URL-decoded!
*/
$config['uri_protocol']	= 'REQUEST_URI';
 

1.REQUEST_URI

REQUEST_URI模式是CI框架默认URL模式,采用分析$_SERVER[‘REQUEST_URI’]来区分控制器/动作/参数。例如: var =value

$_SERVER[‘REQUEST_URI’]值: index.php?/user/login?var=value

实现代码如下:

$uri = parse_url('
$query = isset($uri['query']) ? $uri['query'] : '';
$uri = isset($uri['path']) ? $uri['path'] : '';
if (isset($_SERVER['SCRIPT_NAME'][0]))
{
 if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
 {
 $uri = (string) substr($uri, strlen($_SERVER['SCRIPT_NAME']));
 }
 elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
 {
 $uri = (string) substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
 }
}
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct
// URI is found, and also fixes the QUERY_STRING server var and $_GET array.
if (trim($uri, '/') === '' && strncmp($query, '/', 1) === 0)
{
 $query = explode('?', $query, 2);
 $uri = $query[0];
 $_SERVER['QUERY_STRING'] = isset($query[1]) ? $query[1] : '';
}
else
{
 $_SERVER['QUERY_STRING'] = $query;
}
parse_str($_SERVER['QUERY_STRING'], $_GET);
 

REQUEST_URI 模式的基础上添加了重写规则的支持,可以去掉URL 地址 里面的入口文件index.php

<IfModule mod_rewrite.c> 
RewriteEngine on 
 RewriteCond  %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
ewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
 

这样,就可以直接访问:

2.QUERY_STRING

QUERT_STRING 模式就是传统GET传参数来指定当前访问的模块和操作,例如: `

c参数表示控制器,m参数表示控制器方法(这些参数都是可以配置的),后面的表示其他GET参数。

3.PATH_INFO

PATHINFO模式下的URL访问地址是:

Apache默认支持PATH_INFO, Nginx则需要改造一下:

location ~ ^(.+\.php)(.*)$ {
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_split_path_info ^(.+\.php)(.*)$;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param PATH_INFO $fastcgi_path_info;
 include fastcgi_params;
}
 

PATHINFO安全漏洞: (此漏洞已经修复,访问会出现Access denied)

TP

TP(3.2)框架中可以选择四种URL模式。

1.普通模式

普通模式 也就是传统的GET传参方式来指定当前访问的模块和操作,例如:

m参数表示模块,c参数表示控制器,a参数表示操作(当然这些参数都是可以配置的),后面的表示其他GET参数。

如果默认的变量设置和你的应用变量有冲突的话,你需要重新设置系统配置,例如改成下面的:

'VAR_MODULE' => 'module', // 默认模块获取变量
'VAR_CONTROLLER' => 'controller', // 默认控制器获取变量
'VAR_ACTION' => 'action', // 默认操作获取变量
 

上面的访问地址则变成:

2.PATHINFO模式

PATHINFO模式 是TP3.2默认URL模式,PATHINFO模式下面的URL访问地址是:

PATHINFO地址的前三个参数分别表示模块/控制器/操作

3.REWRITE模式

REWRITE模式 是在PATHINFO模式的基础上添加了重写规则的支持,可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule  ^(.*)$ index.php/$1 [L]
</IfModule>
 

接下来,就可以用下面的URL地址访问了:

4.兼容模式

兼容模式 是用于不支持PATHINFO的特殊环境,URL地址是:

兼容模式配合Web服务器重写规则的定义,可以达到和REWRITE模式一样的URL效果。

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L]
</IfModule>
 

就可以和REWRITE模式一样访问下面的URL地址访问了:

文章来源:智云一二三科技

文章标题:PHP开发常见URL模式,你了解几个?

文章地址:https://www.zhihuclub.com/77752.shtml

关于作者: 智云科技

热门文章

评论已关闭

20条评论

  1. Thankfulness to my father who told me about this blog, this webpage is actually remarkable.

  2. For newest news you have to pay a quick visit internet and on web I found this website as a finest web
    page for latest updates.

  3. I was suggested this website by my cousin. I’m not sure whether this post is written by him as nobody
    else know such detailed about my difficulty. You are incredible!
    Thanks!

  4. There’s certainly a lot to learn about this issue. I like all
    the points you made.

  5. Howdy! This article couldn’t be written any better!
    Looking at this article reminds me of my previous roommate!
    He constantly kept preaching about this. I’ll send this article to him.
    Pretty sure he’s going to have a great read. Thanks for sharing!

  6. Hey thеre! Ꭰo youu қnow iif tһey maҝe any plugins to һelp with SEO?
    І’m trying tto get mү blog to rank for some targeted keywords ƅut Ι’m nott seеing
    veгу ցood gains. If you knoᴡ of ɑny ρlease share.

    Apreciate іt!

    Αlso visit my website;

  7. Heya i’m foг tһe primary time here. I found this board аnd I find It really helpful
    & it helped me оut muⅽh. I’m hoping to provide sometһing back аnd aid otһers like yоu helped
    me.

    my page …

  8. I know this web site presents quality based articles and other data, is there any other
    site which provides these stuff in quality?

  9. This site was… how do you say it? Relevant!!
    Finally I’ve found something that helped me. Thanks
    a lot!

  10. You’re so cool! I do not suppose I’ve read a single thing like this before.
    So good to discover someone with some unique
    thoughts on this topic. Really.. many thanks for starting this up.
    This website is something that’s needed on the web,
    someone with some originality!

  11. Hmm is anyone else having problems with the images on this blog loading?

    I’m trying to figure out if its a problem on my end or if it’s the blog.
    Any feedback would be greatly appreciated.

  12. This ɑrticⅼe will aѕsist the internet
    vіewers for setting up nnew website orr еven a blog from stаrt
    to end.

  13. You actually make it seem so easy with your presentation but I find this
    matter to be really something which I think I would never understand.
    It seems too complex and extremely broad for me.
    I am looking forward for your next post, I will try to get the hang of it!

  14. It’s nearly impossible to find experienced people on this subject, however, you
    sound like you know what you’re talking about! Thanks

  15. This is very attention-grabbing, You are a very skilled blogger.
    I have joined your feed and look ahead to looking for more of your great post.

    Also, I’ve shared your website in my social networks

  16. deepthroat, blowjob, anal, amatureporn, facefuck, baldpussy, asstomouth, assfucking, bbw, bbc, bigcock, bigass,
    teenass, teenfuck, bigtits, titfuck, footjob,thighjob, blackcock, hentai, ecchi, pedophliia, ebony, bigboobs, throatfucking, hardcore,
    bdsm, oldandyoung, masturbation, milf, missionary, nudist, oralsex, orgasm, penetration, pussylicking, teenporn, threesome, whores, bokep, bokepindonesia, bokepterbaru, bokepindonesiaterbaru, bokepterupdate,
    porno, pornoindonesia, pornoterbaru, pornoterupdate

  17. Whats up very nice website!! Man .. Beautiful .. Amazing ..
    I will bookmark your blog and take the feeds additionally?
    I’m satisfied to search out numerous helpful info here in the submit, we want
    work out more techniques in this regard, thanks for sharing.
    . . . . .

  18. Remarkable issues here. I am very glad to see your article.
    Thanks so much and I am looking forward to contact you.

    Will you please drop me a e-mail?

  19. Hi! Quick questiоn thɑt’s comρletely off topic.
    Do you know һow to make your site mobile friendlү?

    My weblog looks weіrd when viewing from my iphone 4.
    I’m trying to find a template or plugin that mіght be able to resolvе this problem.
    If you have any recоmmendations, please share. With thankѕ!

  20. Having read this I believed it was very enlightening.
    I appreciate you finding the time and energy to put this
    content together. I once again find myself personally spending a
    significant amount of time both reading and posting comments.
    But so what, it was still worthwhile!

网站地图