的.htaccess 301重定向单页重定向、htaccess

2023-09-02 00:21:25 作者:许沵半夏丿温柔

在重新设计网站,我有几个需要被重定向的页​​面。一切都停留在同一个域,只是一对夫妇的事情已经重组和/或重新命名。它们的形式是:

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路径常规的前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