使用相同的文件夹和重定向“.htaccess"文件名

编程入门 行业动态 更新时间:2024-10-23 08:23:45
本文介绍了使用相同的文件夹和重定向“.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:

www.example/example

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

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

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

Redirect 301 /example www.example/example.php

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

everything in that folder also gets redirected, so a URL called www.example/example/product.php would now show up as www.example/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"文件名

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

发布评论

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

>www.elefans.com

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