使用相同的文件夹和重定向“.htaccess"文件名 [英] Redirecting “.htaccess” with same folder & file name

查看:54
本文介绍了使用相同的文件夹和重定向“.htaccess"文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前,我重建了一个位于 CMS 上的站点.正如预期的那样,Google 搜索控制台会定期提供生成 404 抓取错误的 URL,我会立即使用 .htaccess 文件上的 301 重定向到它们的新等效项.

A while ago I rebuilt a site that was on a CMS. As expected the Google search console regularly comes up with URLs that generate 404 crawling errors which I would promptly redirect to their new equivalents using a 301 on a .htaccess file.

问题是我遇到了一个看起来像这样的旧页面的 URL:

The issue is that I came across the URL of an old page which looked like this:

http://www.example.com/example 

新页面现在称为 http://www.example.com/example.php.

但是,也有一个名为 example 的文件夹,所以如果我添加这样的重定向:

There is, however, a folder called example too so if I add a redirect like this:

Redirect 301 /example http://www.example.com/example.php

该文件夹中的所有内容也会被重定向,因此名为 http://www.example.com/example/product.php 的 URL 现在将显示为 http://www.example.com/example.php/product.php.

everything in that folder also gets redirected, so a URL called http://www.example.com/example/product.php would now show up as http://www.example.com/example.php/product.php.

有没有一种方法可以成功重定向旧 URL 而不必重命名文件夹 example?

Is there a way to redirect the old URL successfully without having to rename the folder example?

推荐答案

请清除浏览器缓存,然后将以下代码放在主根 .htaccess 文件中:

Please clear your browser cache and then put the following code at main root .htaccess file :

DirectorySlash Off 
RewriteEngine on 
RewriteCond %{THE_REQUEST} \s/+example [NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)  /$1.php [R=302,L]

上面的代码将停止 directoryslash 并使其 off 所以没有斜线的文件不会成为默认目录,然后检查 example存在于 QUERY_STRING 中,技巧就在这一行 RewriteCond %{REQUEST_FILENAME}\.php -f 中,它会检查是否添加了 .php对于这个字符串,它真的是目录中的文件吗?并且我们已经在 root 中有 example.php 所以它会转到最后一行将任何通过上述规则的字符串重定向到 itself.php 和你想要的.

The code above will stop directoryslash and make it off so no file without slash goes to be directory as default and then check if example exists in QUERY_STRING and the trick comes here in this line RewriteCond %{REQUEST_FILENAME}\.php -f,it will check if adding .php to this string , is that really file in directory or not ? and already we have example.php in root so , it will go to last line to redirect any string that passed the rules above to itself.php and that what you want exactly.

上面的代码只会在 root 目录中使用 example 捕获任何 example 但如果想继承其他子文件夹或应用此规则到整个站点,所以,只要有没有扩展名的字符串,优先级将是文件,将以下代码放在您的主根 .htaccess 文件中:

The above code will catch any example in only root directory with example only but if wanna inherit other sub-folders or apply this rule to entire site so , whenever there is string without extension the priority will be for the file , put the following code at your main root .htaccess file :

DirectorySlash Off 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)  /$1.php [R=302,L]

测试这段代码后,如果没问题,将302改为301为永久重定向

After testing this code , if it is Ok , change 302 to 301 to be permanent redirection

这篇关于使用相同的文件夹和重定向“.htaccess"文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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