htaccess将子文件夹重定向到根目录

编程入门 行业动态 更新时间:2024-10-19 23:26:14
本文介绍了htaccess将子文件夹重定向到根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这让我很沮丧,我找不到相同的问题,也许我只是在搜索错误.有人搞砸了,我们有一堆Google链接到一个不存在的子文件夹.因此,我尝试在htaccess中进行301重定向以解决该问题.

This has been frustrating me and I can't find the same issue, maybe I am just searching wrong. Someone messed up and we have a bunch of Google links to a subfolder that does not exist. So I am trying to do a 301 redirect in htaccess to sort that out.

问题是我似乎无法使重定向工作超过一个文件夹.

The problem is I can't seem to get the redirect to work more than one folder deep.

示例. example/subfolder/someOtherFolder 重定向到 example/someOtherFolder 很好.

Example. example/subfolder/someOtherFolder redirects to example/someOtherFolder just fine.

但是 example/subfolder/someOtherFolder/yetAnother 保持不变页面并返回404.

However example/subfolder/someOtherFolder/yetAnother stays on the same page and returns a 404.

这是我的整个.htaccess文件的最后一个变种,它返回上述结果,除上述内容外,我尝试过的所有操作均未返回任何内容.

This is the last variation of my entire .htaccess that returns the above results, nothing I've tried has returned anything but the above.

<IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteRule ^sitefiles/(.*)/$ /$1 [R=301] RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php </IfModule>

有帮助吗? (我讨厌.htaccess>.<)

Help? (I hate .htaccess >.<)

推荐答案

此规则存在问题:

RewriteRule ^sitefiles/(.*)/$ /$1 [R=301]

此规则必需所请求URI上的斜杠

This rule required a trailing slash on the requested URI

相反:

RewriteRule ^sitefiles/(.*) /$1 [R=301]

请注意,还有一个Redirect指令可以进行简单的重定向.就您而言:

Note that there's also a Redirect directive to do simple Redirects. In your case:

Redirect /sitefiles /

最后,请注意,整个Rewrite块可以替换为:

Finally, note that the entirety of that Rewrite block can be replaced with:

FallbackResource /index.php

因此,最终建议的(最佳实践)配置将是:

Thus, the final recommended (best practice) configuration would be:

Redirect /sitefiles / FallbackResource /index.php

完全不用重写.

更多推荐

htaccess将子文件夹重定向到根目录

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

发布评论

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

>www.elefans.com

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