简体   繁体   中英

htaccess redirect subfolder to root

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. http://example.com/subfolder/someOtherFolder redirects to http://example.com/someOtherFolder just fine.

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

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>

Help? (I hate .htaccess >.<)

The problem is with this rule:

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

This rule required a trailing slash on the requested URI

Instead:

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

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

Redirect /sitefiles /

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

Rather than using Rewrite at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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