Question

I have looked around and I have not been able to find the answer to my exact question.

I am not sure if this is the best folder structure to have on my server but this is what it looks like essentially

  • /
    • .htaccess
    • []CSS
    • []Images
    • index.php
    • []Other Sites
      • [] site 1
      • [] site 2
        • index.html
        • [] sub.site2.com
          • index.php

Perhaps obviously:

mySite.com goes to the root. Site1.com goes to the site1.com sub-folder. Site2.com goes to the site2.com sub-folder. And sub.site2.com is a subdomain that goes to the sub folder within site 2.

My problem is that I can not get the .htaccess in the root folder to strip the .html and .php from the all of the sites and sub-sites. This is what I have so far:

DirectoryIndex index.php index.html

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

The above code doesn't allow me to access the "site 2", but affectively strips the .php off the root site. Is this a bad way to approach my folder structure? Can I make the .htaccess in my root affect all the folders, or do I need separate .htaccess files in each sub folder.

Also, the links within the site look like this

<a href="about">About</a>

rather than

<a href="about.php">About</a>

with the htaccess file working properly is that correct?

Was it helpful?

Solution

problem is that I can not get the .htaccess in the root folder to strip the .html and .php from the all of the sites and sub-sites.

This is because the htaccess file in the root folder is outside of the document root for the site1.com, site2.com, etc sites. They can't traverse any further up than the "site1" folder.

Additionally, your rule will never work because your condition is only applied to the first rule, the second rule has free reign to rewrite everything and cause a loop. You need to duplicate your conditions:

DirectoryIndex index.php index.html

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Since your root htaccess file is completely unaccessible by the site1.com/site2.com/etc sites, you need to duplicate those rules in each of your sites.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top