.htaccess redirect FROM subfolder to domain name


.htaccess redirect FROM subfolder to domain name



I had website content in a subfolder (http://mydomain.com/subfolder/index.php), now I copied everything over to the root folder (http://mydomain.com/index.php).



I would like to redirect all the visitors that have bookmarked the old page to the new content (at least to the new index.php) using .htaccess.



Is this correct:


RewriteEngine on
RewriteRule /subfolder/^(.*)$ http://mydomain.com [R=301,L]



?



And where would I place the .htaccess file, in the subfolder or the root folder?




3 Answers
3



Placing the following .htaccess in / (where your index.php is located) should do the trick:


.htaccess


/


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



Or you could place the following .htaccess in /subfolder:


.htaccess


/subfolder


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



Note that the () around .* and the $1 redirects /subfolder/someFile.php to /someFile.php. If you skip it, everything in /subfolder redirects to /.


()


.*


$1


/subfolder/someFile.php


/someFile.php


/subfolder


/





I added the file to the root folder, but the redirection doesn't work.
– user2001714
Feb 6 '13 at 15:12





I found this solution: forums.digitalpoint.com/threads/… and it seems to work.
– user2001714
Feb 6 '13 at 16:24






It's essencially the same solution as above. I'll include it as an option
– mariusnn
Feb 7 '13 at 21:47






And I realized that I had a opening slash on the rule that is not supposed to be there. Both are updated to match both cases. (And includes some 'magic' to keep the files to reference appropriatly.
– mariusnn
Feb 7 '13 at 23:05





@user2001714: Its works!
– VKGS
Jan 30 '14 at 7:56



Try:


RewriteEngine On
RewriteRule ^subfolder/index.php$ /index.php[NC,L,R]



Thanks to @mariusnn in the comments, I was able to solve this issue.



❌ Not Working: .htaccess redirect FROM subfolder to domain name


RewriteEngine on

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



✅ Working: .htaccess redirect entire subdomain "/subdomain/" and files "/subdomain/file_01.php" within are redirected to domain name "example.com"


RewriteEngine on

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



*Note that the () around .* and the $1 redirects /subfolder/someFile.php to /someFile.php. If you skip it, everything in /subfolder redirects to /.


()


.*


$1


/subfolder/someFile.php


/someFile.php


/subfolder


/






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Extract Id from Twitch Clip URL

Why are these constructs (using ++) undefined behavior in C?

I'm Still Waiting (Diana Ross song)