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

查看:391
本文介绍了的.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?

推荐答案

<一个href="http://httpd.apache.org/docs/2.2/mod/mod%5Falias.html#redirectmatch"><$c$c>RedirectMatch使用用来匹配的URL路径常规的前pression。而你的正常的前pression /contact.php 只是意味着的任何包含URL路径 /contact.php 的但不是的的任何URL路径,这正是 /contact.php 的。所以使用锚的开始和结束的字符串( ^ $)

RedirectMatch uses a regular expression that is matched against the URL path. And your regular expression /contact.php just means any URL path that contains /contact.php but not just any URL path that is exactly /contact.php. So use the anchors for the start and end of the string (^ and $):

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

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

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