.htaccess 301 单页重定向 [英] .htaccess 301 redirect of single page

查看:28
本文介绍了.htaccess 301 单页重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站重新设计后,我有几个页面需要重定向.一切都保留在同一个域中,只是重组和/或重命名了一些内容.它们的形式如下:

After a site redesign, I've got a couple of pages that need to be redirected. Everything is staying on the same domain, just a couple of things have been reorganised and/or renamed. They are of the form:

/contact.php

/contact.php

现在:

/contact-us.php

/contact-us.php

使用 .htaccess 文件,我添加了这一行,这是我最推荐的:

Using the .htaccess file, I've added this line, which is the one I find recommended most:

RedirectMatch 301 /contact.php /contact-us.php

这基本上没问题 - 它可以完成工作 - 问题是,它还重定向:

This is mostly fine - it does the job - the problem is, it also redirects:

  • /team1/contact.php
  • /non-existant-folder/contact.php

有没有办法指定我只想重定向根目录中的contact.php?

Is there a way of specifying that I only want to redirect the contact.php in the root?

推荐答案

RedirectMatch 使用与 URL 路径匹配的正则表达式.并且您的正则表达式 /contact.php 仅表示 包含 /contact.php 的任何 URL 路径,但不只是 包含 任何 URL 路径正是 /contact.php.所以在字符串的开头和结尾使用锚点(^$):

RedirectMatch 301 ^/contact.php$ /contact-us.php

这篇关于.htaccess 301 单页重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆