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
完全不用重写.