seesoe
seesoe

Reputation: 402

htaccess redirect and alias root folder

I have a folder in webroot called foo at .com/foo/*/. I'm trying to do a few things. I want the contents of the folder foo to be accessible via .com/bar/* and .com/bar/baz/*. However I also want to redirect traffic from .com/foo/* to .com/bar/* so that users migrate to the new endpoint. The idea is to not break current users with the app open as well.

I have these rules working when separate, however when using them at the same time I just get a redirect loop. I believe things should be combined more with proper flags but I can't seem to figure out the proper combinations.

RewriteRule ^foo/(.*)$ /bar/$1 [R=302,NC,L]

RewriteRule ^bar/baz/(.*)$ bar/$1 [QSA]
RewriteRule ^bar/(.*)$ foo/$1 [QSA]

Edit 1:

.com/bar/ and .com/bar/baz/ will internally point correctly to the real directory foo.

However '.com/barand.com/bar/bazwill not point correctly to the real directoryfoo`. Are the rules fixable or would forcing trailing slash be better?

RewriteRule ^foo/(.*)$ /bar/$1 [R=307,NC,L]

RewriteRule ^bar/baz/(.*)$ foo/$1 [NC,END]
RewriteRule ^bar/(.*)$ foo/$1 [NC,END]

Upvotes: 2

Views: 96

Answers (1)

anubhava
anubhava

Reputation: 785176

If you are using Apache 2.4+ then you can use END:

RewriteEngine On

RewriteRule ^foo/(.*)$ /bar/$1 [R=302,NC,L]

RewriteRule ^bar/baz/(.*)$ bar/$1 [NC,END]

RewriteRule ^bar/(.*)$ foo/$1 [NC,END]

If you are using old Apache e.g. 2.2 etc then use:

RewriteCond %{THE_REQUEST} \s/+foo/ [NC]
RewriteRule ^foo/(.*)$ /bar/$1 [R=302,NC,L]

RewriteRule ^bar/baz/(.*)$ bar/$1 [NC,L]

RewriteRule ^bar/(.*)$ foo/$1 [NC,L]

Upvotes: 2

Related Questions