301重定向.htaccess之谜

时间:2016-07-08 11:25:19

标签: wordpress apache .htaccess redirect

我正在WordPress中建立一个网站,我的客户已经决定要在发布后更改页面名称和别名

old:http://www.successfulspeakernow.com/speech-doctor

新:http://www.successfulspeakernow.com/executive-speech-doctor

Google已经知道旧网址,因此我们需要在.htaccess文件中编写301重定向。

我尝试了几件事,但无济于事,所以任何人都可以告诉我下面的.htaccess文件有什么问题(我复制了文件的全部内容):

# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# hieronder van niet-www naar www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# hieronder van http naar https
# RewriteCond %{HTTPS} off
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RedirectMatch 301 ^/speech-doctor/ http://www.successfulspeakernow.com/executive-speech-doctor/
RedirectMatch 301 ^/speech-doctor-written-review-plus-2-hour-consult http://www.successfulspeakernow.com/executive-speech-doctor/written-review-plus-2-hour-consult
RedirectMatch 301 ^/speech-doctor-written-review-only http://www.successfulspeakernow.com/executive-speech-doctor/written-review-only
RedirectMatch 301 ^/speech-doctor-2-hour-consult http://www.successfulspeakernow.com/executive-speech-doctor/2-hour-consult
</IfModule>

# END WordPress


AddHandler application/x-httpd-php56 .php .php5 .php4 .php3

任何帮助,非常感谢!

汤姆

3 个答案:

答案 0 :(得分:0)

我测试了你建议的RedirectMatch规则,它们在我的测试系统上运行得很好。此外,你还没有真正说过什么不起作用。是否重定向?抛出错误?转到原始页面?不断循环?等

确保您的浏览器缓存干净?打开/关闭重定向将需要一个干净的浏览器缓存,否则浏览器将自动使用它已经知道的重定向。

另外,如果你在wordpress中这样做,那么可以在.htaccess文件中执行它,但是如果使用插件则会更容易

https://wordpress.org/plugins/redirection/

希望有所帮助,

你好 今天进一步了解一下,您的问题可能与您的服务器上没有mod_alias有关。上面的@jordi是正确的

RedirectMatch位于mod_alias中,其中RewriteRule位于mod_rewrite中。如果我禁用了mod_alias,则服务器为RedirectMatch指令返回404,以及apache日志中的此错误     /.htaccess:无效的命令'RedirectMatch',可能拼写错误或由服务器配置中未包含的模块定义

使用Jordi Nebots的回复是准确的。

答案 1 :(得分:0)

请勿在正则表达式上使用前导斜杠。另外,我会选择尾部斜杠。

RedirectMatch 301 ^speech-doctor-written-review-plus-2-hour-consult/? http://www.successfulspeakernow.com/executive-speech-doctor/written-review-plus-2-hour-consult
RedirectMatch 301 ^speech-doctor-written-review-only/? http://www.successfulspeakernow.com/executive-speech-doctor/written-review-only
RedirectMatch 301 ^speech-doctor-2-hour-consult/? http://www.successfulspeakernow.com/executive-speech-doctor/2-hour-consult
RedirectMatch 301 ^speech-doctor/? http://www.successfulspeakernow.com/executive-speech-doctor/

修改:我已将您的第一个301移至最后一个位置,以避免在没有尾随斜线的情况下访问该网址时与其他规则发生冲突。

请尝试将以下四条规则移至RewriteBase

RewriteBase /
# Move 301 redirects here #
RewriteRule ^index\.php$ - [L]

要回答您的评论:我已经删除了第一条规则,因为如果(例如)您访问了该网址:

  

http://www.successfulspeakernow.com/speech-doctor-2-hour-consult

此网址符合^speech-doctor-2-hour-consult/?^speech-doctor/?规则。因此,如果RedirectMatch 301 ^speech-doctor/?是您的第一个重定向规则,那么http://www.successfulspeakernow.com/speech-doctor-2-hour-consult将被重定向到http://www.successfulspeakernow.com/executive-speech-doctor/,因为规则是从上到下处理的。

答案 2 :(得分:0)

作为替代方案,您是否尝试使用RewriteRule进行重定向?这就是我通常做的事情......

RewriteRule ^speech-doctor-written-review-plus-2-hour-consult/? http://www.successfulspeakernow.com/executive-speech-doctor/written-review-plus-2-hour-consult [R=301,L]
RewriteRule ^speech-doctor-written-review-only/? http://www.successfulspeakernow.com/executive-speech-doctor/written-review-only [R=301,L]
RewriteRule ^speech-doctor-2-hour-consult/? http://www.successfulspeakernow.com/executive-speech-doctor/2-hour-consult [R=301,L]
RewriteRule ^speech-doctor/? http://www.successfulspeakernow.com/executive-speech-doctor/ [R=301,L]

此外,我假设您已经检查过您在Apache上启用mod_rewrite并且您的.htaccess文件正在处理中,对吧?