301重定向apache显示错误的url

时间:2016-09-19 10:07:45

标签: apache .htaccess url mod-rewrite

事实证明,由于不同的原因,我必须更改我的网站网址,旧的网址已在Google中编入索引,我希望您浏览它们,新内容会以新网址显示。

我补充说:

Redirect 301 /library/books/pitz-lower.php http://www.example.com/library/pitz-lower.php

但是要从谷歌获取,浏览器中的网址如下所示:

http://www.example.com/library/pitz-lower.php?url=library/pitz-lower

htaccess的

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On


RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]




RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /library/library\.php\?url=([^\s]+) [NC]
RewriteRule ^ /library/%1.php? [L,R=301]
RewriteCond %{REQUEST_URI} !^/library/library\.php
RewriteRule ^library/(.+)\.php$ /library/library.php?url=$1 [L]

</IfModule>

#r301

Redirect 301 /library/books/pitz-lower.php http://www.example.com/library/pitz-lower.php

我不明白为什么会这样,有人帮我一把?谢谢!!

1 个答案:

答案 0 :(得分:1)

有两个问题:

  1. 混合mod_rewrite规则和mod_alias规则(即Redirect指令)并不好,因为这两个模块在不同时间运行。
  2. 在内部重写规则之前保留301重定向规则。
  3. 您修改后的.htaccess应为:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^library/books/pitz-lower\.php$ /library/pitz-lower.php [L,NC,R=301]
    
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /library/library\.php\?url=([^\s]+) [NC]
    RewriteRule ^ /library/%1.php? [L,R=301]
    
    RewriteCond %{REQUEST_URI} !^/library/library\.php
    RewriteRule ^library/(.+)\.php$ /library/library.php?url=$1 [L,QSA]
    

    请记住在测试时清除浏览器缓存。