.htaccess 301 单页重定向

编程入门 行业动态 更新时间:2024-10-28 07:22:41
本文介绍了.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 单页重定向

本文发布于:2023-10-31 17:11:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!