How do you hide the subfolder using a htaccess file?

I am aware that this question has already asked in one way or another, but nor the question description nor the answers were working/satisfactory. So I try to explain the issue as clearly as possible.

SAMPLE FOLDER STRUCTURE

Read More
.htaccess
index.php
[subfolder-dog]
---- index.php
---- image.jpg
---- [animals]
--------- index.php
--------- cat.jpg
--------- dog.jpg
--------- frog.jpg

SAMPLE DOMAIN

  • www.dog.com

INTENSION

Guest visiting the website at www.dog.com, should be presented with the subfolder content without ever showing the subfolder in the URL. To make this more clear. http://www.dog.com/image.jpg should show the image and keep that in the URL, not that suddenly it will show http://www.dog.com/subfolder-dog/image.jpg.

HTACCESS (SO FAR)

# Turn on Apache mod_rewrite
RewriteEngine on 
Options +FollowSymlinks 
RewriteBase /

# IF THE HOST COMES FROM THE DOMAIN WWW.DOG.COM
RewriteCond %{HTTP_HOST} ^www.dog.com$ [NC]

# AND IF THE REQUESTED URL DOES NOT ALREADY CONTAIN THE SUBFOLDER-DOG
RewriteCond %{REQUEST_URI} !^/subfolder-dog/.*$ [NC]

# REWRITE BY PREPENDING EVERY REQUEST WITH SUBFOLDER-DOG
RewriteRule (.*) subfolder-dog/$1 [L]

RESULT

At first sight, this will work as expected.

The big problem only occurs on directories without trailing slashes.

This wil become extremely annoying when you try and install WordPress! When putting all the WordPress files within the subfolder-dog folder and going to http://www.dog.com all seems normal. When clicking on “Create configuration file” button, the address bar now says http://www.dog.com/subfolder-dog/wp-admin/setup-config.php. When you correct the URL by removing the subfolder from the URL it requests a file again and it works again.

I’ve been stuck on this issue for a week now, hopefully someone can shine a light on this subject. It’s starting to make me crazy.

Related posts

Leave a Reply

1 comment

  1. Try the following:

    RewriteEngine On
    RewriteRule ^$ subfolder-dog/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ subfolder-dog/$1
    

    OR

    RewriteEngine On
    RewriteBase /
    
    # Add trailing slash if path does not contain a period or end with a slash
    RewriteCond %{REQUEST_URI} !(.|/$)
    RewriteRule (.*) http://www.yoursite.com/$1/ [R=301,L]
    
    #Change http://yoursite.com to http://www.yoursite.com (Optional)
    RewriteCond %{HTTP_HOST} ^yoursite.com$
    RewriteRule ^/?(.*)$ http://www.yoursite.com/$1 [R=301,L]
    
    #Rewrites http://www.yoursite.com/subdir to http://www.yoursite.com/
    RewriteCond %{REQUEST_URI} !^/subdir
    RewriteRule ^(.*)$ subdir/$1 [L]
    

    Hope this help you!!!