2

So basically i want 2 folders in root one is server and the other is public.

Folder server is secured by always goto server/routes.php

Folder public is free to goto wherever if that file or dir exist.

If URL is localhost/server(anything) it goes to the server/routes.php with the URL (anything)

If URL is localhost/(anything but server) and (anything but server) is not a file or a dir in public it goes to the public/index.html with the URL (anything but server) otherwise if (anything but server) is a file or a dir in public it goes to public/(anything but server) i.e. (a file or a dir)

Some exmaples:

  • localhost/server
    • server/routes.php
    • $_SERVER['REQUEST_URI'] is empty
  • localhost/server/users
    • server/routes.php
    • $_SERVER['REQUEST_URI'] is users
  • localhost/server/users?orderby=name
    • server/routes.php
    • $_SERVER['REQUEST_URI'] is users?orderby=name
  • localhost
    • url is empty
    • public/index.html
  • localhost/users
    • url is users
    • public/index.html
  • localhost/partials/users.html (that file exist in public/partials/users.html)
    • public/partials/users.html

How would I do this logic in .htaccess?

1 Answer 1

4

Place this .hatccess on the folder server:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /server/

RewriteRule ^routes\.php$ - [L]
RewriteRule . routes.php [L]
RewriteRule ^$ routes.php [L]

And place this .htaccess on the root folder of your domain:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/public/$1 !-d [NC]
RewriteCond %{DOCUMENT_ROOT}/public/$1 !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/index.html [L]

RewriteCond %{DOCUMENT_ROOT}/public/$1 -d [NC,OR]
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteCond %{REQUEST_URI} !^/public/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]

Give this 2 set of .htaccess a try and keep in mind that if you have tried anything else early using 301 redirects its advisable that you clear your browser cache or use a different browser that you haven't used to browse those sites to avoid the cache.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow thanks works perfectly. I noticed a problem with these set of rule which I asked for, that was a little hard to track down. If I were to load a file in index.html that does not exist it would cause a infinite loop(I guess), that hangs the browser. So I added RewriteCond %{REQUEST_URI} !\.(.*)$ before first RewriteRule. It make sure that if I try to load a nonexistence file it will just pass RewriteCond and result in 404.
Well if you have access to the httpd.conf you can enable the rewritelog which makes debugging of rewrite rule very simple as you can also define the level of data it logs. So its easy to see where and why it fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.