Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect folder to subfolder

Tags:

.htaccess

Notice: I wrote the solution. Thank you anyway. :)

I know it seems duplicated question: I searched a lot, I applied tens of rewrite rules but they don't work.

I need to redirect from

www.domain.com/folder1

to

www.domain.com/folder1/subfolderA

Note: folder1 is emtpy. in subfolderA I have index.html, index.php, css file, js files. If I go to "www.doman.com/folder1/subfolderA/" I see a simple web page with correct css and js provided by index.php file.

My .htaccess file is in www.domain.com/ (I've tried to put it in folder1 but it doesn't work, even removing "RewriteBase /" line). File permssions: 0755.

"Option -Indexes","Option +Indexes" generate error "Option indexes not allowed here", so I deleted them.
"Options +FollowSymLinks" generates error "Option FollowSymLinks not allowed here" and I deleted it.

First try: endless url

RewriteBase /
RewriteRule ^/folder1(/.*)?$ /folder1/subfolderA$1 [L,R]
(rif: https://stackoverflow.com/a/32525136/1315873) ('R' is just for debug)


Results:
www.domain.com/folder1/subfolderA/subfolderA/subfolderA/subfolderA/subfolderA/

Second try: adding a condition to stop endless loop:

RewriteBase /
RewriteCond %{HTTP_HOST} !subfolderA [NC]
RewriteRule ^/folder1(/.*)?$ /folder1/subfolderA$1 [L,R]

Perfect for http://htaccess.madewithlove.be/ but not for my server. Results:
www.domain.com/folder1/subfolderA/subfolderA/subfolderA/subfolderA/subfolderA (unchanged)

Third try: using $ after 'folder1'

RewriteBase /
RewriteCond %{HTTP_HOST} !subfolderA [NC]
RewriteRule ^/folder1/$ /folder1/subfolderA/ [L,R]

Results:
Forbidden
Log says: "No matching DirectoryIndex (index.html,index.htm,index.php,index.php3,index.phtml,index.shtml,index.wml) found, and server-generated directory index forbidden by Options directive"

If I add to the url "index.php" or "index.html" the result is "404 Not found" (that's true actually).

I don't know what else to do. I hope in your help.
It's an impossibile redirect to do?
Thank you.

like image 588
Fil Avatar asked Nov 18 '25 01:11

Fil


2 Answers

I find the right way to do :P The .htaccess file look like this:

RewriteBase /
RewriteCond %{REQUEST_URI} !subfolderA [NC]
RewriteRule ^/?folder1(/.*)?$ /folder1/subfolderA$1 [L,R]

As I don't wont to change url in browser (one must see "www.domain.com/folder1/"), I remove ",R" from last line.

I hope it can be of help to someone else.

like image 199
Fil Avatar answered Nov 20 '25 15:11

Fil


You can create an index.php or index.html page in folder1 that will redirect users to subfolderA

e.g index.php can contain the following code

              <?php
                header("location: subfolderA");
                 ? >
like image 27
The Nerdy Geek Avatar answered Nov 20 '25 14:11

The Nerdy Geek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!