PHP中的URL重写 [英] URL Rewriting in PHP

查看:66
本文介绍了PHP中的URL重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用PHP开发的网站,现在我差不多完成了它,但只是想让这个URL用户友好。我的网站是动态的,并且在动态创建的链接中有URL中的查询字符串,所以现在我如何才能使其成为用户友好的URL。



例如我的网址我是提供a标签,例如 http://www.example.com/product.php?product_name=abc_pqr&product_id=452



那我怎么能把它作为



http://www/example.com/product/abc_pqr/452



我知道可以使用.htaccess文件来完成,但我对URL重写非常新,就像新手一样。

I have a site developed in php and now I am almost at the end of launching it but just want to make the URL user-friendly. My site is dynamic and in links which is dynamically created has query string in URL so now how can I make it user-friendly URL.

For example my url which I give in "a" tag like http://www.example.com/product.php?product_name=abc_pqr&product_id=452

then how can I make it as

http://www/example.com/product/abc_pqr/452

I know it can be done using .htaccess file but I am very new for URL Rewriting and like a newbie to it.

推荐答案

您可以使用httpd.conf文件中的LoadModule指令将mod_rewrite模块动态加载到Apache Web服务器环境中。在文本编辑器中加载此文件并找到类似于下面给出的行。



#LoadModule rewrite_module modules / mod_rewrite.so



删除#并取消保存httpd.conf文件,取消注释该行。重新启动Apache服务器,如果一切顺利,现在将在您的Web服务器上启用mod_rewrite模块。



让我们使用mod_rewrite重写我们的第一个URL



好​​的,现在服务器上启用了mod_rewrite模块。让我们看看如何让这个模块加载自己并使它适用于我们。



为了动态加载模块,你必须添加一行到您的.htaccess文件。 .htaccess文件是配置文件,其中定义了Apache指令,它们为网站提供分布式目录级配置。在您的Web服务器测试目录中创建.htaccess文件 - 或者您要激活URL重写的任何其他目录 - 并在其中添加以下给定行。



RewriteEngine on



现在我们打开了重写引擎,Apache已经准备好为你重写URL了。让我们看看一个示例重写指令,用于向服务器发送请求,以便在服务器级别将first.html重定向到second.html。将以下给定的行与我们之前添加的RewriteEngine指令一起添加到.htaccess文件中。



RewriteRule ^ first.html
You can make the mod_rewrite module load dynamically in to the Apache web server environment using the LoadModule Directive in the httpd.conf file. Load this file in a text editor and find a line similar to the one given below.

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment this line by removing the # and save the httpd.conf file. Restart your Apache server and if all went well mod_rewrite module will now be enabled on your web server.

Lets Rewrite our first URL using mod_rewrite

Ok, now the mod_rewrite module is enabled on your server. Lets have a look at how to make this module load itself and to make it work for us.

In order to load the module dynamically you have to add a single line to your .htaccess file. The .htaccess files are configuration files with Apache directives defined in them and they provide distributed directory level configuration for a website. Create a .htaccess file in your web servers test directory - or any other directory on which you want to make URL Rewriting active - and add the below given line to it.

RewriteEngine on

Now we have the rewrite engine turned on and Apache is ready to rewrite URLs for you. Lets look at a sample rewrite instruction for making a request to our server for first.html redirected to second.html at server level. Add the below given line to your .htaccess file along with the RewriteEngine directive that we have added before.

RewriteRule ^first.html


second.html



我将在下一部分解释我们在这里做了什么,但是如果一切顺利,那么对你的first.html做出的任何请求服务器将转移到second.html。这是URL重写的最简单形式之一。



这里需要注意的是重定向完全隐藏在客户端之外,这与经典的HTTP重定向不同。客户端或浏览器的印象是从first.html获取second.html的内容。这使网站能够生成具有客户意识的即时URL,并且使URL重写非常强大。



mod_rewrite模块的基础知识



现在我们知道可以通过使用.htaccess文件为整个网站或特定目录启用mod_rewrite,并在前面的示例中完成了基本的重写指令。在这里,我将解释我们在第一次重写样本时究竟做了什么。



Mod_rewrite模块为URL重写和RewriteRule指令提供了一组配置指令语句 - 我们在上一个样本中看到 - 是最重要的一个。 mod_rewrite引擎使用模式匹配替换来进行翻译,这意味着对正则表达式的良好掌握可以帮助你很多。



注意:正则表达式如此庞大它们不符合本文的范围。我将在某一天尝试写另一篇关于该主题的文章。



1. RewriteRule指令



RewriteRule的一般语法非常简单。 RewriteRule模式替换[标志]



模式部分是重写引擎在要捕获的传入URL中查找的模式。所以在我们的第一个样本中,^ first.html
second.html

I will explain what we have done here at the next section, but if all went well then any requests for first.html made on your server will be transferred to second.html. This is one of the simplest forms of URL Rewritting.

A point to note here is that the redirect is kept totally hidden from client and this differs from the classic HTTP Redirects. The client or the browser is given the impression that the content of the second.html is being fetched from first.html. This enables websites to generate on the fly URLs with out the clients awareness and is what makes URL Rewriting very powerful.

Basics of mod_rewrite module

Now we know that mod_rewrite can be enabled for an entire website or a specific directory by using .htaccess file and have done a basic rewrite directive in the previous example. Here I will explain what exactly have we done in the first sample rewrite.

Mod_rewrite module provides a set of configuration directive statements for URL Rewriting and the RewriteRule directive - that we saw in the previous sample - is the most important one. The mod_rewrite engine uses pattern-matching substitutions for making the translations and this means a good grasp of Regular Expressions can help you a lot.

Note: Regular Expressions are so vast that they will not fit in to the scope of this article. I will try to write another article on that topic someday.

1. The RewriteRule Directive

The general syntax of the RewriteRule is very straightforward. RewriteRule Pattern Substitution [Flags]

The Pattern part is the pattern which the rewrite engine will look for in the incoming URL to catch. So in our first sample ^first.html


是模式。该模式被写为正则表达式。



替换是对URL中捕获的模式进行的替换或转换。在我们的示例中,second.html是替换部分。



标志是可选的,它们使重写引擎除了在URL上进行替换之外还执行某些其他任务串。如果存在的标志用方括号定义,并且应用逗号分隔。



让我们看看更复杂的重写规则。请看下面的网址。



http://yourwebsite/articles.php?category = stamps& id = 122



现在我们将上面的URL转换为搜索引擎和用户友好的URL,如下所示。



http:// yourwebsite / articles / stamps / 122



使用以下代码创建一个名为articles.php的页面:


is the Pattern. The pattern is written as a regular expression.

The Substitution is the replacement or translation that is to be done on the caught pattern in the URL. In our sample second.html is the Substitution part.

Flags are optional and they make the rewrite engine to do certain other tasks apart from just doing the substitution on the URL string. The flags if present are defined with in square brackets and should be separated by commas.

Lets take a look at a more complex rewrite rule. Take a look at the following URL.

http://yourwebsite/articles.php?category=stamps&id=122

Now we will convert the above URL in to a search engine and user friendly URL like the one given below.

http://yourwebsite/articles/stamps/122

Create a page called articles.php with the following code:


这篇关于PHP中的URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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