加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中重写URL?

发布时间:2020-12-13 13:38:02 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试在我的 PHP应用程序中实现URL重写.有人可以分享PHP和MySQL中实现URL重写的一步一步的过程吗? 在我的应用程序中,我想实现以下URL重写,我想重定向 1. http://example.com/videos/play/google-io-2009-wave-intro2. http://example.com/videos/play/
我正在尝试在我的 PHP应用程序中实现URL重写.有人可以分享PHP和MySQL中实现URL重写的一步一步的过程吗?

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

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重写.

根据SEO,管理,应用程序的观点,以下两种类型的URL最好还是一个.

1. http://example.com/videos/play/google-io-2009-wave-intro
2. http://example.com/videos/play/203/google-io-2009-wave-intro
A Beginner’s Guide to mod_rewrite.

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

只是为了好玩,这是一个Kohana .htaccess文件重写:

# 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=subjectsu0026amp;b=php.

如果您发现这些URL有吸引力,我会鼓励您进一步了解MVC框架(模型,视图,控制器).它本质上允许您像一组功能对待您的网站:

www.mysite.com/jokes

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

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

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

注意第一个正斜杠如何调用一个函数,所有这些都会填充该函数的参数.这真的非常好,使网络开发更加有趣!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读