首页 > 运 维 > apache > 正文

实站:apache下实现301永久性重定向的方法
2014-02-20 10:10:11   来源:   评论:0 点击: 收藏

实现123.com全部重定向 (跳转)到www.123.com。同时按照google的建议,使用服务器端 301 重定向,为了确保用户及搜索引擎定向...

         实现123.com全部重定向 (跳转)到www.123.com。同时按照google的建议,使用服务器端 301 重定向,为了确保用户及搜索引擎定向至正确网页的最佳方法。301 状态代码表示某网页已被永久迁移至新位置。下面将了解一下apache下实现301永久性重定向2个方法,需要具有访问服务器的 .htaccess 文件的权限。



 

1. Apache模块 mod_alias的 Redirect 和 RedirectMatch命令

上面提到2个命令使用方法相似。而区别就是后者RedirectMatch基于正则表达式匹配对当前的URL发送一个外部重定向语法为:

Redirect [status] URL-path URL

RedirectMatch [status] regex URL

status参数可以使用以下HTTP状态码:

permanent

返回一个永久性重定向状态码(301),表示此资源的位置变动是永久性的。

temp

返回一个临时性重定向状态码(302),这是默认值。

seeother

返回一个“参见”状态码(303),表示此资源已经被替代。

gone

返回一个“已废弃”状态码(410),表示此资源已经被永久性地删除了。如果指定了这个状态码,则URL参数将被忽略。

举例:

APACHE

Redirect 301 /old/old.htm http://www.123.com/new.htm
Redirect permanent /one http://123.com/two
RedirectMatch 301 (.*).gif$ http://www.123.com/images/$1.jpg


2.使用mod_rewrite重写URL方式

APACHE

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^123.com
RewriteRule ^(.*)$ http://www.123.com/$1 [R=permanent,L]


在这里判断当前服务器变量HTTP_HOST是否等于1230.com,为真就进行重写,按照R=permanent进行永久重定向,L表示并立即停止重写操作,并不再应用其他重写规则

下面是apache最终实现的.htaccess文件,同时也并入ecshop重写规则。

// ################################################

<FilesMatch "\.(bak|inc|lib|sh|tpl|lbi|dwt)$">
    order deny,allow
    deny from all
</FilesMatch>
<IfModule mod_rewrite.c>

RewriteEngine On
#RewriteBase /

#Redirect
Options +FollowSymLinks
RewriteCond %{HTTP_HOST}   ^123.com$
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^(.*)$ http://www.123.com/$1 [R=301,L]


# direct one-word access
RewriteRule ^index\.html$    index\.php [L]
RewriteRule ^temai\.html$    temai\.php [L]
RewriteRule ^pinpai\.html$    index\.php [L]
RewriteRule ^category$      index\.php [L]

# access any object by its numeric identifier
RewriteRule ^feed-c([0-9]+)\.xml$       feed\.php\?cat=$1 [L]
RewriteRule ^feed-b([0-9]+)\.xml$       feed\.php\?brand=$1 [L]
RewriteRule ^feed-type([^-]+)\.xml$       feed\.php\?type=$1 [L]
RewriteRule ^feed\.xml$                 feed\.php [L]

RewriteRule ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$  category\.php\?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$                            category\.php\?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$                                      category\.php\?id=$1&brand=$2&page=$3&sort=$4&order=$5 [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$                                                       category\.php\?id=$1&brand=$2&page=$3  [QSA,L]
RewriteRule ^category-([0-9]+)-b([0-9]+)(.*)\.html$                                                                category\.php\?id=$1&brand=$2  [QSA,L]
RewriteRule ^category-([0-9]+)(.*)\.html$                                                                          category\.php\?id=$1  [QSA,L]

RewriteRule ^goods-([0-9]+)(.*)\.html$  goods\.php\?id=$1 [QSA,L]

RewriteRule ^article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$  article_cat\.php\?id=$1&page=$2&sort=$3&order=$4  [QSA,L]
RewriteRule ^article_cat-([0-9]+)-([0-9]+)-(.+)(.*)\.html$              article_cat\.php\?id=$1&page=$2&keywords=$3 [QSA,L]
RewriteRule ^article_cat-([0-9]+)-([0-9]+)(.*)\.html$                   article_cat\.php\?id=$1&page=$2   [QSA,L]
RewriteRule ^article_cat-([0-9]+)(.*)\.html$                            article_cat\.php\?id=$1   [QSA,L]

RewriteRule ^article-([0-9]+)(.*)\.html$                                article\.php\?id=$1   [QSA,L]

RewriteRule ^brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html   brand\.php\?id=$1&cat=$2&page=$3&sort=$4&order=$5 [QSA,L]
RewriteRule ^brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html                brand\.php\?id=$1&cat=$2&page=$3 [QSA,L]
RewriteRule ^brand-([0-9]+)-c([0-9]+)(.*)\.html                         brand\.php\?id=$1&cat=$2 [QSA,L]
RewriteRule ^brand-([0-9]+)(.*)\.html                                   brand\.php\?id=$1 [QSA,L]

RewriteRule ^tag-(.*)\.html                                             search\.php\?keywords=$1 [QSA,L]
RewriteRule ^snatch-([0-9]+)\.html$                                     snatch\.php\?id=$1 [QSA,L]
RewriteRule ^group_buy-([0-9]+)\.html$                                  group_buy\.php\?act=view&id=$1 [QSA,L]
RewriteRule ^auction-([0-9]+)\.html$                                    auction\.php\?act=view&id=$1 [QSA,L]

RewriteRule ^exchange-id([0-9]+)(.*)\.html$                             exchange\.php\?id=$1&act=view [QSA,L]
RewriteRule ^exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ exchange\.php\?cat_id=$1&integral_min=$2&integral_max=$3&page=$4&sort=$5&order=$6 [QSA,L]
RewriteRule ^exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$                         exchange\.php\?cat_id=$1&page=$2&sort=$3&order=$4 [QSA,L]
RewriteRule ^exchange-([0-9]+)-([0-9]+)(.*)\.html$                                          exchange\.php\?cat_id=$1&page=$2  [QSA,L]
RewriteRule ^exchange-([0-9]+)(.*)\.html$                                                   exchange\.php\?cat_id=$1  [QSA,L]



########################################################################

下载地址 http://www.cnop.net/uploadfile/2014/0220/20140220102526261.zip

相关热词搜索:apache 实现 永久性

上一篇:apache+php完美解决301重定向的两种方法
下一篇:Apache配置httpd-vhosts虚拟主机总结及注意事项