Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache rewriterule to redirect folder to subfolder without loop

Long story short, I am trying for the last 3 hours to perform what seems to be a basic url redirect. All the website requests to mysite.com/folder should be redirected to mysite.com/folder/subfolder/. The redirect should be performed "only!" if the user enters /folder. If he accesses /folder/file no redirect should happen.

I have tried the following but with no success, all attempts cause an infinite loop:

RewriteRule ^/folder$ /folder/subfolder/ [R]
Redirect 301 /folder /folder/subfolder/

I don't have access to the vhost file, neither am I allowed to perform this using php or a similar 'workaround'. Either way, htaccess should be the best place to do this. Can anybody shade a light on what I am doing wrong?

like image 389
Biggie Mac Avatar asked Jan 18 '23 20:01

Biggie Mac


1 Answers

redirect use a prefix pattern matching, so instead you should use RedirectMatch with a regex, so that you will match only the directory access and not the file access in the subdirectory. You do not need the rewriteRule, which is doing the same thing.

This should work:

RedirectMatch ^/folder/$ /folder/subfolder/

Be careful, you used a 301 Redirect, 301 means permanent, so you may need to close your browser before testing the new rule, your browser has memorized the redirect permanent answer and will not ask it again until you close it.

like image 52
regilero Avatar answered Jan 30 '23 21:01

regilero