1

I am struggling with an htaccess file. Actually, it is two files.
But first things first, so my questions are:

1) Why does my .htaccess(1) file add the www at the beginning of the HTTP_HOST and the slash at the end of folder REQUEST_URI IF AND ONLY IF the .htaccess(2) file is not there (deleted or renamed)?
2) What is wrong with the RewriteRule and conditions that I wrote in .htaccess(2) to redirect the REQUEST_URI to /publicfolder/REQUEST_URI? Conditions doesn't seem to work and when I surf to domain.com/nonpublicfolder it goes to domain.com/domainfolder/publicfolder/nonpublicfolder.

My website is structured as follows:

/
    .htaccess(1)
    domainfolder/
        .htaccess(2)
        publicfolder/
            genericfolder/
            index.extention
            file.extention
        nonpublicfolder/

So I have one htaccess file in the root folder ( .htaccess(1) ) where I:

  • add 'www' at the beginning of the HTTP_HOST;
  • add '/' at the end of REQUEST_URI if it does not end with a file extension;
  • redirect domain.com/anyfolder/anyfile.extention to domain.com/domainfolder/anyfolder/anyfile.extention;

like so:

<IfModule mod_rewrite.c>

# System symbolic links are allowed.
Options +FollowSymlinks

# Runtime rewriting engine enabled.
RewriteEngine On

# HTTP_HOST starts with 'www'.
RewriteCond %{HTTP_HOST} ^(?!www\.) [NC]
RewriteRule ^.*$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NC]

# Folder requests end with '/'. 
RewriteCond %{REQUEST_URI} ![^/]+\.[a-zA-Z0-9]+$ [NC]
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,NC]

# Files and folders are in the 'domainfolder' folder.  
RewriteCond %{REQUEST_URI} !^/?domainfolder/ [NC]
RewriteRule ^.*$ /domainfolder%{REQUEST_URI} [R=301,NC]

</IfModule>

And then I have my .htaccess(2) file - in the domainfolder folder - where I redirect files and folders requests to the publicfolder folder IF AND ONLY IF they are not pointing to the notpublicfolder folder or to the Google Site Verification file:

<IfModule mod_rewrite.c>

# Runtime rewriting engine enabled.
RewriteEngine On

# Public files and folders are in the 'publicfolder' folder. 
RewriteCond %{REQUEST_URI} !^/?domainfolder/publicfolder/ [NC]
RewriteCond %{REQUEST_URI} !^/?domainfolder/nonpublicfolder/ [NC] 
RewriteCond %{REQUEST_URI} !^/?domainfolder/googlexxx.html$ [NC]
RewriteRule ^/?(.+)$ /publicfolder/$1 [R=301,NC,L]

</IfModule>

Thank you very much for your time and patience.

  • This is a well framed Q. However, my answer is slightly more general because it would simply take me too long to research a detailed answer. However, I think that I've given you enough pointers now to crack it yourself. Post back with specifics _after trying_ and we can discuss :-) – TerryE Jun 10 '14 at 12:41

1 Answers1

0

(1) mod_rewrite does multiple passes and on each by default it will only open the first .htaccess file it finds walking up the folder hierarchy from the requested file. read my Tips for debugging .htaccess rewrite rules for more discussion of this. Yes you can use a Options setting to change this behaviour but this has a performance hit and I would suggest that you avoid doing so.

(2) When using hierachical .htaccess files, mod_rewrite has to associate URI path to the current directory and can get this wrong. The RewriteBase directive tells mod_rewrite what the true association is, so use this.

Rule order is important. If you don't have a local Apache instance where you have root privilege and can enable rewrite logging, you need to build up your access file(s) incrementally rule-by-rule, testing at each step because you only get a work/doesn't work return. Again my tips explains how to do this.

Community
  • 1
  • 1
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • Thank you very much @TerryE for your answer. I read your Tips thoroughly and I have to say I have considered all of them while building my`.htaccess` files. In fact, I worked on a single `.htaccess` file before I decided that splitting rules actually made more sense to me. And everything worked just fine. Now I lose rules effectiveness when redirecting to the second `.htaccess` file. How come? I don't get it. The only thing I haven't tried yet is defining the RewriteBase. But this should only help me with the conditions I can't seem to get working, right? – user2949795 Jun 10 '14 at 16:22