首页 > Linux系统 > system > 用mod_rewrite组件创建搜索引擎友好链接
2011
08-03

用mod_rewrite组件创建搜索引擎友好链接

Apache的mod_rewrite组件可以创建很多对搜索引擎友好的链接,我们最熟知的就是创建静态化地址,比如http://www.example.com/thread-111.html这样的格式,以利于搜索引擎收录,增加网站权限和排名。下面还列出了一些其他应用:

避免重复内容:

RewriteEngine On
# Redirect non-www to www.
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Redirect direct requests for "/index.html" to "/"
RewriteRule ^(([^/]+/)*)index\.html http://www.example.com/$1 [R=301,L]

转向定位URL:

RewriteEngine On
# Redirect relocated old urls to the new urls
RewriteRule ^oldpage\.html$ http://www.example.com/newpage.html [R=301,L]
RewriteRule ^directory/oldpage\.html$ http://www.example.com/directory/newpage.html [R=301,

# Redirect relocated directories to the new directories
RewriteRule ^olddir/(.*) http://www.example.com/newdir/$1 [R=301,L]

转向不存在的内链:

RewriteEngine On

#+---------------------------------
#+ - BEGIN: URL fixup redirect routine
#+---------------------------------

# Get the requested URI, query string and hostname.
RewriteCond %{REQUEST_URI} ^(/[^?]*)([?].*)?$
RewriteRule . - [E=myUri:%1,E=myQuery:%2]

# Replace hmtl, htlm, and thml file extensions with html extension.
RewriteCond %{ENV:myUri} ^(.*)[.,](hmtl|htlm|thml)$ [NC]
RewriteRule . - [E=myRedirect:yes,E=myUri:%1.html]

# Replace comma(s) or multiple filetype delimiter periods in the url
# with a single period.
RewriteCond %{ENV:myUri} ^(.*)([,.]{2,}|,)((s?html?|php[1-9]?|aspx?|pdf|xls).*)$ [NC]
RewriteRule . - [E=myRedirect:yes,E=myUri:%1.%3]

# Remove invalid trailing characters
RewriteCond %{ENV:myUri} ^([/0-9a-z._\-]*)[^/0-9a-z._\-] [NC]
RewriteRule . - [E=myRedirect:yes,E=myUri:%1]

# Remove trailing punctutation
RewriteCond %{ENV:myUri} ^(.*)[._\-]+$
RewriteRule . - [E=myRedirect:yes,E=myUri:%1]

# Do the external 301 redirect only if needed and the resource exists
# at the corrected URI.
RewriteCond %{ENV:myRedirect} ^yes$ [NC]
RewriteCond %{DOCUMENT_ROOT}%{ENV:myUri} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:myUri} -d
RewriteRule . http://www.example.com%{ENV:myUri}%{ENV:myQuery} [R=301,L]

#+---------------------------------
#+ - END: URL fixup redirect routine
#+---------------------------------

SEO友好链接:

比如我们的默认URL地址是:http://www.example.com/widgets.php?cat=blue&page=1,现在想改成http://www.example.com/widgets/blue/page1.php,则我们可以编写以下.htaccess文件。

RewriteEngine On
RewriteRule ^widgets/(.*)/page(.*)\.php /widgets.php?cat=$1&page=$2 [L]
最后编辑:
作者:admin
捐 赠如果您觉得这篇文章有用处,请支持作者!鼓励作者写出更好更多的文章!

留下一个回复