Tyler Lazenby
Tyler Lazenby

Reputation: 451

Hide a parent directory from a URL, or allow links not to require that parent directory

I have a PHP based website that I would like to make a little more secure and more SEO friendly. My site has two folders private and site. All public facing pages, css, scripts, images are in the site folder. All my backend PHP code is in the private folder. Currently I am protecting any access to the private folder by using Deny from all in an .htaccess file that is located in the private folder. On my local server (currently using XAMPP) the project is located in my htdocs folder and has a project name we will say is example_project. Under the site folder I have a few other sub directories sorting out the CSS, JS, images, and a few main sub categories of the pages in my site. I am trying very hard to figure out how to make it so that (A) the URL will NOT need to include site in the URL to access anything located under it and (B) it will change any requests including site in the URL to be rewritten to exclude it from the path.

(A) Example example_project/subfolder/index.php accesses example_project/site/subfolder/

(B) Example example_project/site/subfolder/index.php redirects to example_project/subfolder/

I have an .htaccess file in nearly every directory. In my top level of my project (note that this is not the server web root) I have the following .htaccess file

<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|png|pdf|swf|txt|php)$">
    #Used to allow for content expiration
    <IfModule mod_expires.c>
        ExpiresActive Off
    </IfModule>
    <IfModule mod_headers.c>
        FileETag None
        Header unset ETag
        Header unset Pragma
        Header unset Cache-Control
        Header unset Last-Modified
        Header set Pragma "no-cache"
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Expires "Mon, 10 Apr 1972 00:00:00 GMT"
    </IfModule>
</FilesMatch>

RewriteEngine On

#Force site to require https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Allow links not to have the .php file extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#THIS IS WHAT I AM HAVING TROUBLE WITH
RewriteRule ^$ site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^site /example_project/site/$1 [NC,L]

EDIT Please note that I just also remembered to add a capture group, so the last line now reads as RewriteRule !^site/([-/\w]+) /hb1/site/$1 [NC,L]

Upvotes: 1

Views: 533

Answers (1)

anubhava
anubhava

Reputation: 785531

Comment out first 2 http->https and .php rules and test with these rules

RewriteEngine On

RewriteRule ^$ site/ [L]

RewriteRule ^(?!site/).* site/$0 [NC,L]

Upvotes: 1

Related Questions