I was taught that it is a good practice to separate publicly viewed webpages from included files and other, more sensitive information.
That poses a problem, however, when you wish to request index.php in the public folder.
Allow me to illustrate:
Site Root/
Includes/
-server_side_validation.php
Public/
-index.php
JS/
-js_form_validation.js
Sandbox/
Etc..
To get to index.php, I have to type in www.mywebsite.com/public/index.php
, which is ugly.
So I learned a little about .htaccess and after a lot of trail and error I came to this code:
AddType application/x-httpd-php55 .php
# turn on rewrite engine
RewriteEngine on
## remove /public from host name
RewriteCond %{HTTP_HOST} ^www.mywebsite.ca$ [NC,OR]
RewriteCond %{HTTP_HOST} ^mywebsite.ca$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
What this apparently does is turn www.mywebsite.com/public/index.php
into www.mywebsite.com
, essentially "hiding" the /public/index.php
part but still requesting index.php.
That all works dandy until I try to execute a pretty routine user registration from my index.php page. It goes like this:
On index.php, I have a registration form and a JS script that validates the form then sends an AJAX request to ../includes/registration_val.php
to validate again on the server side before database submission.
The JS validation file is inside the public folder, inside a JS folder. So one directory deep inside the public folder.
When I attempt to submit the form to the server_side_validation.php
from js_form_validation.js
, I get 404 Not Found
. Upon inspection I see that the request goes to /public/includes/registration_val.php
.
So it seems to imply that my code seems to mistake public/
as the site root instead of actual site root.
Now when I bypass the above .htaccess code, the problem goes away. So somewhere in my .htaccess I'm boning the whole thing up and I'm hoping one of you programmers much smarter than I can come to my rescue so I can get things back on track.
I want to keep that nice little benefit of clearing away the /public/index.php
directory in favor of a nice clean www.mywebsite.com
but without screwing up any internal directory traversing.
Muchas gracias!!