htaccess将子文件夹重定向到根目录 [英] htaccess redirect subfolder to root

查看:93
本文介绍了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.

示例. http://example.com/subfolder/someOtherFolder 重定向到

Example. http://example.com/subfolder/someOtherFolder redirects to http://example.com/someOtherFolder just fine.

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

However http://example.com/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将子文件夹重定向到根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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