chaseshak
chaseshak

Reputation: 313

Mod_Rewrite Simplest Way to Hide Folder in URL For ALL Sub-directories

I've got all my code for my website stored in my document root at mywebfolder. There are many sub-directories, e.g. mywebfolder/deals/.. mywebfolder/user/.. and so on. I'm currently hosting my site with mydomain.com.

I would like to not display mywebfolder at anytime, as it's redundant and makes my URLs unecessary longer and hard to remember. I've got a mod_rewrite running that accomplishes this, but only when i go to mydomain.com. (I.e. it only works for the document root). In my 000-default.conf file I've got the following:

    RewriteEngine On
    <Directory "/var/www/html">
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require valid-user
            RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
            RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
            RewriteCond %{REQUEST_URI} !^/mywebfolder/
            RewriteRule(.*) /mywebfolder/$1
    </Directory>

I understand that my conditions only do the rewrite when I navigate to mydomain.com. Is there anyway I can easily extrapolate these to the entire site so when navigating a user never sees /mywebfolder/ in the URL? Or do I have to create a directory block for EVERY directory?

Upvotes: 1

Views: 162

Answers (1)

Dusan Bajic
Dusan Bajic

Reputation: 10879

Try this:

...
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule /mywebfolder/(.*) /$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/mywebfolder/
RewriteRule /(.*) /mywebfolder/$1

Upvotes: 1

Related Questions