简体   繁体   English

将整个子目录重定向到根目录

[英]Redirect entire sub-directory to root directory

So I am trying to clean up my search engine redirect errors. 因此,我正在尝试清理搜索引擎重定向错误。 I had some old development sites that got indexed that I want to redirect to the main site. 我有一些旧的开发站点已被索引,我想将其重定向到主站点。

Basically I want everything in the /dev/ folder to go to https://myfakewebsite.com/ 基本上,我希望/ dev /文件夹中的所有内容都转到https://myfakewebsite.com/

I added this to .htaccess file in the /dev/ directory: 我将此添加到/ dev /目录中的.htaccess文件中:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /$1 [R=301,L]

I tried this but it doesn't quite work, it does take out the /dev/ but it keeps the rest of the link there. 我试过了,但效果不佳,确实删除了/ dev /,但将其余链接保留在那里。

For example: https://myfakewebsite.com/dev/index.php?route=product/category&path=122 例如: https : //myfakewebsite.com/dev/index.php?route=product/category&path=122

redirects to: https://myfakewebsite.com/index.php?route=product/category&path=122 重定向到: https : //myfakewebsite.com/index.php?route= product/ category&path=122

I want it to redirect to: https://myfakewebsite.com/ (removing the /index.php?route=product/category&path=122 part). 我希望它重定向到: https ://myfakewebsite.com/(删除/index.php?route=product/category&path=122部分)。

Is there a way to do this with .htaccess? 有没有办法用.htaccess做到这一点?

You can use the following rule in your /dev/.htaccess 您可以在/dev/.htaccess使用以下规则

RewriteEngine on


RewriteRule  (.*) / [L,R=301]

The rule above redirects all requests from /dev to the root of site ie . 上面的规则将所有请求从/dev重定向到站点的根目录,即。 /dev/* => / including query strings from /dev?querystring to /?querystring . /dev/* => /包括从/dev?querystring/?querystring By default mod-rewrite appends old query string to the destination path so if you do not want it then you can use QSD flag in your rule or you can another rule to handle and remove QUERY_STRING . 默认情况下,mod-rewrite将旧查询字符串附加到目标路径,因此,如果您不希望使用它,则可以在规则中使用QSD标志,也可以使用另一个规则来处理和删除QUERY_STRING。

You can use the following rules to redirect urls with and without query string to / . 您可以使用以下规则将带有和不带有查询字符串的URL重定向到/ The ? ? at the end of the rule's destination discards the old query string. 规则目标的末尾丢弃旧的查询字符串。

RewriteEngine on

# redirect /dev?querystring to /
RewriteCond %{QUERY_STRING} .+
RewriteRule  (.*) /? [L,R=301]
# redirect /dev/uri to /
RewriteRule  (.*) / [L,R=301]

Make sure to clear your browser cache before testing these redirects. 在测试这些重定向之前,请确保清除浏览器缓存。

You might want to achieve this kind of "trivial" redirect with the RedirectMatch directive from mod_alias , instead of doing this with mod_rewrite . 您可能希望使用mod_aliasRedirectMatch指令来实现这种“简单”的重定向,而不是使用mod_rewrite

RedirectMatch 301 "/dev/(.*)" "/$1"

See documentation at https://httpd.apache.org/docs/2.4/mod/mod_alias.html . 请参阅https://httpd.apache.org/docs/2.4/mod/mod_alias.html上的文档。

You left the match results as part of the redirect on this line: 您将匹配结果留在此行中作为重定向的一部分:

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

Specifically $1 as part of /$1 (the redirecting portion of the rule). 特别是$1作为/$1 (规则的重定向部分)的一部分。 Since this file is within the /dev directory it automatically removes /dev/ then applies the match to the redirect. 由于此文件位于/dev目录中,因此它将自动删除/dev/然后将匹配项应用于重定向。 Since you are using the capture ( $1 ) as part of the rule, it'll take everything it matched after /dev/ (in your rule you should read it as ^/dev/(.*)$ ) and apply it to /$1 , where $1 is whatever it "found" during ^(.*)$ in the previous step. 由于您将捕获( $1 )用作规则的一部分,因此它将获取/dev/之后匹配的所有内容(在您的规则中,应将其读取为^/dev/(.*)$ )并将其应用于/$1 ,其中$1是在上一步中的^(.*)$中“找到”的值。

Try removing the pattern match, eg: 尝试删除模式匹配,例如:

RewriteRule ^(.*)$ / [R=301,L]

Additionally, since you aren't using the results of the match you can remove the parenthesis (eg ^.*$ ). 另外,由于您没有使用匹配结果,因此可以删除括号(例如^.*$ )。 You can also put this .htaccess file in the root and simply redirect ^/dev(/?.*)$ to / depending on how you want to manage your subdirectories. 您也可以将该.htaccess文件放在根目录中,然后根据要管理子目录的方式将^/dev(/?.*)$重定向到/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM