简体   繁体   English

如何在PHP中重写URL?

[英]How to do URL re-writing in PHP?

I am trying to implement URL rewriting in my PHP application. 我正在尝试在我的PHP应用程序中实现URL重写。 Can someone share a step by step procedure of implementing URL rewriting in PHP and MySQL? 有人可以分享在PHP和MySQL中实现URL重写的分步过程吗?

In my application I want to implement following URL rewriting, I want to redirect 在我的应用程序中,我想实现以下URL重写,我想重定向

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

to

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

Please tell me how to implement both URL rewriting in any of the above way. 请告诉我如何以上述任何一种方式实现这两个URL重写。

One more thing which URL will be best according to SEO, management, application point-of-view out of the following two types. 根据以下两种类型中的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入门指南

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. 通常,这只不过是启用mod_rewrite模块(您可能已经在主机上启用了该模块),然后将.htaccess文件添加到您的Web目录中。 Once you've done that, you are only a few lines away from being done. 完成此操作后,您仅需几行之遥即可完成。 The tutorial linked above will take care of you. 上面链接的教程将为您服务。

Just for fun, here's a Kohana .htaccess file for rewriting: 只是为了好玩,这里有一个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]

What this will do is take all requests and channel them through the index.php file. 这将执行所有请求,并通过index.php文件引导它们。 So if you visited www.examplesite.com/subjects/php, you may actually be visiting www.examplesite.com/index.php?a=subjects&b=php. 因此,如果您访问了www.examplesite.com/subjects/php,则实际上您可能正在访问www.examplesite.com/index.php?a=subjects&b=php。

If you find these URLs attractive, I would encourage you to go one step further and check out the MVC Framework (Model, View, Controller). 如果您发现这些URL具有吸引力,我建议您进一步走一步,并查看MVC框架(模型,视图,控制器)。 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)
}

Or, www.mysite.com/jokes/2 或者,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! 这真的非常好,让网络开发变得更加有趣!

You cannot do this with PHP alone. 您不能仅使用PHP来做到这一点。 You'll need to look into mod_rewrite (assuming you are using apache). 您需要研究mod_rewrite(假设您正在使用apache)。

Using mod rewrite: 使用mod重写:

RewriteRule ^videos/play/([0-9]+)/([^.]+)$ play.php?id=$1&name=$2

example.com/play.php?id=203&name=google-io-2009-wave-intro example.com/play.php?id=203&name=google-io-2009-wave-intro

would work as 将作为

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

Keep in mind that redirecting is not the same as rewriting. 请记住,重定向与重写不同。

A redirect is when the server receives the request and the response is sent as a redirect. 重定向是服务器接收到请求并且响应作为重定向发送时。 For instance, you request a page www.example.com/page. 例如,您请求一个页面www.example.com/page。 When the server receives this, either the page or the server self issues a redirect command to the browser. 服务器收到此消息后,页面或服务器自身都会向浏览器发出重定向命令。 The redirect command basically says "go here: new page". 重定向命令基本上说“去这里:新页面”。 In PHP, this is in the form of header('Location: newpage.html'); 在PHP中,它的形式为header('Location:newpage.html');

A rewrite is when the server receives the request from the browser then looks in a list of matching regular expressions for that site. 重写是指服务器从浏览器收到请求后,在该站点的匹配正则表达式列表中查找。 If a match is found, the URL is rewritten into that form and is responded to accordingly. 如果找到匹配项,则将该URL重写为该形式,并相应地进行响应。 For instance, the requested URL www.example.com/specificpage could be rewritten (on the server end) as www.example.com/?loadpage=specificpage. 例如,可以将请求的URL www.example.com/specificpage(在服务器端)重写为www.example.com/?loadpage=specificpage。 The browser never receives header information stating that it must go somewhere else. 浏览器永远不会收到标头信息,表明它必须到其他地方。

If your server supports it (apache / mod_rewrite), you can use a .htaccess file to re-direct the visitor. 如果您的服务器支持它(apache / mod_rewrite),则可以使用.htaccess文件来重定向访问者。

Something like (just a draft, adapt to your own needs...): 有点像(只是草稿,适应您自己的需求...):

RewriteEngine On
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/([0-9]+)/([-0-9a-zA-Z]+)/?$ /$1/$2.php?id=$3 [L]

for the second example. 对于第二个例子。

After reading comments and question once again, I've realized that my previous answer is not completely the thing your are looking for. 再次阅读评论和问题后,我意识到我以前的答案并不完全是您想要的东西。 So here some additions, if you want rewrite/redirect/proxy the URL addresses so for example then user types www.f1.com he will actually see www.f2.com, but domain will remain the same, you probably need to setup a reverse proxy using Apache for that. 因此,这里有一些补充,如果您想重写/重定向/代理URL地址,例如,然后用户键入www.f1.com,他实际上将看到www.f2.com,但是域将保持不变,您可能需要设置一个反向代理使用Apache的。 It act's similar to mod_rewrite. 它的行为类似于mod_rewrite。

Regarding the second part, since modern crawler also performing the URL link analyses I think your first option is better performance wise. 关于第二部分,由于现代爬虫还执行URL链接分析,我认为您的第一个选择是更好的性能。

Previous answer: 先前的答案:
And using PHP you can achieve redirection like: 使用PHP可以实现重定向,例如:

<?php
// Here you can parse the current location
// and decide whenever you want to go
// if $_SERVER[ 'PHP_SELF'] some conditions then....
header('Location: http://www.example.com/');
?>

From php.net . 来自php.net Here more examples. 这里有更多例子。

Sorry about not giving you detailed explanation, but maybe you could take a look at DokuWiki for a starting point. 很抱歉没有给您详细的解释,但是也许您可以看看DokuWiki作为起点。

In its setup it accepts 3 modes: 在其设置中,它接受3种模式:

  1. Regular urls: ?id=zzz 常规网址:?id = zzz
  2. .htaccess rewrite: uses apache to do the rewritting .htaccess重写:使用Apache进行重写
  3. internal (php) rewrite: urls end up looking like http://www.example.com/kb/doku.php/start 内部(php)重写:网址最终看起来像http://www.example.com/kb/doku.php/start

It's free so you can just download and browse the code. 它是免费的,因此您只需下载和浏览代码即可。

As a note, the apache redirect is very likely the best solution but the pure php one is also interesting in case your code is going to run in IIS6 servers where rewritting is not as easy as in apache. 需要注意的是,apache重定向很可能是最好的解决方案,但是纯php也是很有意思的,以防您的代码要在IIS6服务器中运行,而重写在apache中不那么容易。

Unfortunately, it's really up to the web server, not PHP. 不幸的是,这实际上取决于Web服务器,而不是PHP。 Depending on if you use Apache or something else, it has to do this before your script is executed. 根据您使用的是Apache还是其他方式,它必须在执行脚本之前执行此操作。 I use mod_rewrite, and an example rewrite rule looks like this: 我使用mod_rewrite,示例重写规则如下所示:

<Directory "/home/www/html">
    RewriteEngine On
    RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/%{REQUEST_URI} [PT,L]
</Directory>

The above rule basically says if the URL is a valid path to a real file or directory, load it. 上面的规则基本上说,如果URL是到真实文件或目录的有效路径,请加载它。 Otherwise, run it through index.php. 否则,请通过index.php运行它。

You could approach this slightly differently from the suggestions above by using a Front Controller, it's a common solution for making custom URLs and is used in all languages, not just PHP. 您可以通过使用Front Controller来实现与上述建议稍有不同的方法,这是制作自定义URL的常见解决方案,并且可以在所有语言中使用,而不仅限于PHP。 Here's a guide: http://www.oreillynet.com/pub/a/php/2004/07/08/front_controller.html 这是一个指南: http : //www.oreillynet.com/pub/a/php/2004/07/08/front_controller.html

Essentially you would create an index.php file that is called for every URL, its job is to parse the URL and determine which code to run based on the URL's contents. 本质上,您将创建一个为每个URL调用的index.php文件,其工作是解析URL并根据URL的内容确定要运行的代码。 So, for example a user would use a URL such as http://example.com/index.php/videos/play/203/google-io-2009-wave-intro and index.php would extract the remaining elements from the URL (/videos/play/203/google-io-2009-wave-intro) take the first part, load a php file with that name (videos.php or you can make it use play.php) and pass through the parameters 203 and google-io. 因此,例如,用户将使用诸如http://example.com/index.php/videos/play/203/google-io-2009-wave-intro之类的URL,而index.php将从URL中提取其余元素URL(/ videos / play / 203 / google-io-2009-wave-intro)为第一部分,加载具有该名称的php文件(videos.php或可以使用play.php)并传递参数203和google-io。

It's effectively doing the same thing as rewriting the code in mod_rewrite but does have a few benefits: 它实际上在做与mod_rewrite中的代码重写相同的操作,但是确实有一些好处:

  1. Doesn't require too much mod_rewrite code if you are new to that. 如果您是新手,则不需要太多的mod_rewrite代码。
  2. Allows you to put all security code in one place - in index.php 允许您将所有安全代码放在一个位置-在index.php中
  3. It's easy to change URLs and handle 404s etc. 更改URL和处理404等很容易。

You can remove index.php from the URL using mod_rewrite, see here: http://www.wil-linssen.com/expressionengine-removing-indexphp/ 您可以使用mod_rewrite从网址中删除index.php,请参见此处: http : //www.wil-linssen.com/expressionengine-removing-indexphp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM