Search code examples
.htaccesshttp-redirecthttpsvirtualfilesystem

.htaccess / https redirection, virtual folder access with exceptions


/* THIS would appear to be a duplicate of another question I had asked 10 days back (.htaccess / redirecting to https://www.example.com and virtual folder access), but (a) I didnt get any responses to the changes I made to the question, and (b) this question here is a more complex version of the above and I believe warrants a new query */

I have a site which has the following entities: example.com/index.html example.com/main.php example.com/SPECIAL/main.php besides the above, I have folders like example.com/images/, /css/, etc which the html generated by the scripts point to.

I need .htaccess magic for ALL of the below:

(a) permanently redirecting users from "example.com" to "www.example.com" (301 stuff)

(b) permanently redirecting users from http://www.example.com to https://www.example.com (301 stuff)

(c) users accessing example.com/ should, of course, see index.html, but if they type
     example.com/ABCDEF OR
     example.com/ABCDEF/
they should be redirected to example.com/main.php?folder=ABCDEF
EXCEPT when ABCDEF is a real folder (like /SPECIAL/, /images/, /css/, etc)

(d) If user tries to access example.com/REALFOLDER/ (eg /images/), he should just be redirected to example.com/

(e) All of this should not mess with the viewing of example.com/index.html

Sorry if this sounds complex, but thats exactly what I need!

S


Solution

  • Try these rules:

    # (a) and (b)
    RewriteCond %{HTTP_HOST} =example.com [OR]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
    # (d)
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule . / [L,R]
    # (c)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ main.php?folder=$1 [L]