如何在PHP中重写URL?

编程入门 行业动态 更新时间:2024-10-26 06:39:02
本文介绍了如何在PHP中重写URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在PHP应用程序中实现URL重写.有人可以分享在PHP和MySQL中实现URL重写的分步过程吗?

I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in PHP and MySQL?

在我的应用程序中,我想实现以下URL重写,我想重定向

In my application I want to implement following URL rewriting, I want to redirect

1. example/videos/play/google-io-2009-wave-intro 2. example/videos/play/203/google-io-2009-wave-intro

1. example/videos/play.php?title=google-io-2009-wave-intro 2. example/videos/play.php?id=203

请告诉我如何通过上述任何一种方式来实现这两个URL重写.

Please tell me how to implement both URL rewriting in any of the above way.

从以下两种类型中,根据SEO,管理和应用程序的观点来看,哪个URL最好是另一件事.

One more thing which URL will be best according to SEO, management, application point-of-view out of the following two types.

1. example/videos/play/google-io-2009-wave-intro 2. example/videos/play/203/google-io-2009-wave-intro

推荐答案

入门指南进行mod_rewrite .

通常,这只不过是启用mod_rewrite模块(您可能已经在主机上启用了该模块),然后将.htaccess文件添加到您的Web目录中.完成此操作后,您仅需几行之遥即可完成.上面链接的教程将为您服务.

Typically this will be nothing more than enabling the mod_rewrite module (you likely already have it enabled with your host), and then adding a .htaccess file into your web-directory. Once you've done that, you are only a few lines away from being done. The tutorial linked above will take care of you.

只是为了好玩,下面是一个 Kohana .htaccess文件进行重写:

Just for fun, here's a Kohana .htaccess file for rewriting:

# Turn on URL rewriting RewriteEngine On # Installation directory RewriteBase /rootDir/ # Protect application and system files from being viewed RewriteRule ^(application|modules|system) - [F,L] # Allow any files or directories that exist to be displayed directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite all other URLs to index.php/ RewriteRule .* index.php/$0 [PT,L]

这将执行所有请求,并通过index.php文件引导它们.因此,如果您访问了www.examplesite/subjects/php,则实际上您可能正在访问www.examplesite/index.php?a=subjects&b=php.

What this will do is take all requests and channel them through the index.php file. So if you visited www.examplesite/subjects/php, you may actually be visiting www.examplesite/index.php?a=subjects&b=php.

如果您发现这些URL有吸引力,我鼓励您更进一步,并查看MVC框架(模型,视图,控制器).从本质上讲,它使您可以将网站视为一组功能:

If you find these URLs attractive, I would encourage you to go one step further and check out the MVC Framework (Model, View, Controller). It essentially allows you to treat your website like a group of functions:

www.mysite/jokes

www.mysite/jokes

public function jokes ($page = 1) { # Show Joke Page (Defaults to page 1) }

或者,www.mysite/jokes/2

Or, www.mysite/jokes/2

public function jokes ($page = 1) { # Show Page 2 of Jokes (Page 2 because of our different URL) }

请注意第一个正斜杠如何调用一个函数,随后的所有填充该函数的参数.真的非常好,让网络开发变得更加有趣!

Notice how the first forward slash calls a function, and all that follow fill up the parameters of that function. It's really very nice, and make web-development much more fun!

更多推荐

如何在PHP中重写URL?

本文发布于:2023-11-02 08:35:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   如何在   PHP   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!