如何在 PHP 中进行 URL 重写? [英] How to do URL re-writing in PHP?

查看:22
本文介绍了如何在 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. http://example.com/videos/play/google-io-2009-wave-intro
2. http://example.com/videos/play/203/google-io-2009-wave-intro

1. http://example.com/videos/play.php?title=google-io-2009-wave-intro
2. http://example.com/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. http://example.com/videos/play/google-io-2009-wave-intro
2. http://example.com/videos/play/203/google-io-2009-wave-intro

推荐答案

初学者指南修改改写.

通常,这只不过是启用 mod_rewrite 模块(您可能已经在主机上启用了它),然后将 .htaccess 文件添加到您的网络目录中.一旦你这样做了,你离完成只有几行了.上面链接的教程会照顾你.

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.com/subjects/php,您实际上可能正在访问 www.examplesite.com/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.com/subjects/php, you may actually be visiting www.examplesite.com/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.com/jokes

www.mysite.com/jokes

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

或者,www.mysite.com/jokes/2

Or, www.mysite.com/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 重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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