URL Rewrite技术 URL Rewrite技术。该技术的核心方程是在服务器端,主要通过服务器的重写技术,一种基于正则表达式的重定向技术。 原理如:index.php?id=124这个地址,服务器事先定义,/可以替换,重写为?id=则,这个地址就变成index.php/124。这个定义的意思是说,当服务器www.twomaxinter.com接受到一个http请求,请求访问其上的index.php/124这个文件时,先通过Rewrite对该地址进行重写,然后载入重写后的地址,进行解析。这样当你访问http://www.twomaxinter.com/index.php/124时,并没有直接到达这个文件,而是这个地址,在服务器那里被换掉了,而这个换掉的过程,对客户端而言是隐蔽的,不可知的。 这里说的,只是原理,简单的替换,事实上,URL Rewrite还可以做许多更复杂的代换。如喜悦国际村的,由showthreads.php?postid=12345转换为12345.html。具体可以参照apache手册中的说明。 为什么要应用URL Rewrite技术。 从原理上可知,这种技术并不能实际提高访问速度,减少负载,与生成静态页面有着本质上的区别,但为什么有那么多网站热衷于此呢?说到这里,我们不得不提到搜索引擎。搜索引擎后台的蜘蛛引擎可以动态抓取互联网上的页面,经分析后存入数据库。但动态页面,一般却是不被蜘蛛引擎考虑的,只有很少部分,可能会被自动收录进去。而蜘蛛引擎识别静态页面和动态页面是从一点基本点着手,即静态页面一般不包含参数。即?id=...&post=..之类的。而动态页面多有这个东西。所以蜘蛛引擎在收录过程中,?后的部分基本是不考虑的。这样造成的结果就是showthreads.php?id=123456由于?后部分被忽略,就变成了showthreads.php。当然这个页面并不具备任何实际内容的。即,URL Rewrite技术多为搜索引擎而来。 具体的URL Rewrite方法,参数,可参阅apache手册中URL Rewrite部分。及以下地址: http://linux.dalouis.com/doc/apache2.0/misc/rewriteguide.html 希望对各位,能够有所帮助。 Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1191944 给个代码大家参考:Some quick stuff: ([a-z]+) - just small letters ([A-Z]+) - just big letters ([0-9]+) - just numbers ([a-zA-Z_-]+) - small and big letters and minus (-) ([a-zA-Z0-9_-]+) - small and big letters, numbers and minus (-) (.*) - everything - warning!!!! don't ever use - this have to much permissions Example 1. www.domain.com/index.php?category=search-engine-optimization 1. solution: Code:htaccess tips Part 2. How to rewrite advanced url's on apache Part 1. is here: http://forums.seochat.com/showthread.php?p=121601 I have test all this. Use absolute paths for everything, example:
results: www.domain.com/search-engine-optimization 2. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?category=$1 [R=301,L]
results: www.domain.com/category/search-engine-optimization 3. solution Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)$ index.php?category=$1 [R=301,L]
results: www.domain.com/search-engine-optimization.html 4. solution Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+).html$ index.php?category=$1 [R=301,L]
results: www.domain.com/category/search-engine-optimization.html 5. solution Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+).html$ index.php?category=$1 [R=301,L]
results: www.domain.com/category/search-engine-optimization/ Example 2. www.domain.com/index.php?category=internet&subcategory=search-engine-optimization 1. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)/$ index.php?category=$1 [R=301,L]
results: www.domain.com/internet/search-engine-optimization 2. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?category=$1&subcategory=$2 [R=301,L]
results: www.domain.com/category/internet/subcategory/search-engine-optimization 3. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)/subcategory/([a-zA-Z0-9_-]+)$ index.php?category=$1&subcategory=$2 [R=301,L]
results: www.domain.com/internet/search-engine-optimization.html 4. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ index.php?category=$1&subcategory=$2 [R=301,L]
results: www.domain.com/category/internet/subcategory/search-engine-optimization.html 5. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)/subcategory/([a-zA-Z0-9_-]+).html$ index.php?category=$1&subcategory=$2 [R=301,L]
results: www.domain.com/category/internet/subcategory/search-engine-optimization/ Example 3. www.domain.com/index.php?category=internet&subcategory=search-engine-optimization&more=articles 1. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)/subcategory/([a-zA-Z0-9_-]+)/$ index.php?category=$1&subcategory=$2 [R=301,L]
results: www.domain.com/internet/search-engine-optimization/articles 2. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?category=$1&subcategory=$2&more=$3 [R=301,L]
results: www.domain.com/category/internet/subcategory/search-engine-optimization/more/articles 3. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)/subcategory/([a-zA-Z0-9_-]+)/more/([a-zA-Z0-9_-]+)$ index.php?category=$1&subcategory=$2&more=$3 [R=301,L]
results: www.domain.com/internet/search-engine-optimization/articles.html 4. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ index.php?category=$1&subcategory=$2&more=$3 [R=301,L]
results: www.domain.com/category/internet/subcategory/search-engine-optimization/more/articles.html 5. solution: Code:Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)/subcategory/([a-zA-Z0-9_-]+)/more/([a-zA-Z0-9_-]+).html$ index.php?category=$1&subcategory=$2&more=$3 [R=301,L]
results: www.domain.com/category/internet/subcategory/search-engine-optimization/more/articles/ more resources: Apache mod rewrite docs Apache RewriteBase docs Mod rewrite forum Absolute and Relative paths SEW mod rewrite tips and tricks url rewrite概述 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news.asp?id=234 这样的链接映射成 news/234.html,从外面看上去和静态链接一样。 当我需要将将news.asp?id=234的映射成news/234.html时,只需设置: RewriteRule /news/(/d+)/.html /news/.asp/?id=$1 [N,I] 这样就把 /news/234.html 这样的请求映射成了 /news.asp?id=234 当有对/news/234.html的请求时:web服务器会把实际请求转发给/news.asp?id=234 而在IIS也有相应的REWRITE模块:比如ISAPI REWRITE和IIS REWRITE,语法都是基于正则表达式,因此配置几乎和apache的mod_rewrite是相同的: 比对于某一个简单应用可以是: RewriteRule /news/(/d+)/.html /news/news/.php/?id=$1 [N,I] 这样就把 http://www.52net.com/news/234.html 映射到了 http://www.52net.com/news/news.php?id=234 一个更通用的能够将所有的动态页面进行参数映射的表达式是: 把 http://www.myhost.com/foo.php?a=A&b=B&c=C 表现成 http://www.myhost.com/foo.php/a/A/b/B/c/C 。 RewriteRule (.*?/.php)(/?[^/]*)?/([^/]*)/([^/]*)(.+?)? $1(?2$2&:/?)$3=$4?5$5: [N,I] 通过URL REWRITE还有一些好处: 隐藏后台实现:这在后台应用平台的迁移时非常有用:当从asp迁移到java平台时,对于前台用户来说,根本感受不到后台应用的变化; 简化数据校验:因为像(/d+)这样的参数,可以有效的控制数字的格式甚至位数; 比 如我们需要将应用从news.asp?id=234迁移成news.php?query=234时,前台的表现可以一直保持为 news/234.html。从实现应用和前台表现的分离:保持了URL的稳定性,而使用mod_rewrite甚至可以把请求转发到其他后台服务器上。Options +FollowSymLinks RewriteEngine On RewriteRule ^category/([a-zA-Z0-9_-]+)/subcategory/([a-zA-Z0-9_-]+)/more/([a-zA-Z0-9_-]+)/$ index.php?category=$1&subcategory=$2&more=$3 [R=301,L]
你可以发表评论、引用到你的网站或博客,或通过RSS 2.0订阅这个博客的所有文章。
上一篇: php中文乱码终极解决方案
|
下一篇:windows隐藏磁盘分区