0

I want to create a folder structure for my PHP project like in the image.

enter image description here

Public folder has the UI code files and src folder has the back-end code files. But the problem is when I want to visit the page from browser, I have to put the folder name in the URL.

Like if I want to go to home.php I'll have to type http://localhost/public/home.php. So I want to remove the /public from my url so it should look like http://localhost/home.php

So is there a way where I can map the file's path to URL and I can visit that URL from browser?

Bunny Fox
  • 23
  • 1
  • 1
  • 4

1 Answers1

0

You could use a htaccess if you're using apache to make this work.

the code would be:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ /public/$1.php

Then the url for your home.php would be domain.tld/home

Explanation:
Options +FollowSymLinks
Options FollowSymLinks enables you to have a symlink in your webroot pointing to some other file/dir. With this disabled, Apache will refuse to follow such symlink

RewriteEngine on
Turns on the RewriteEngine :) so you are able to use rewrite conditions and rules etc.

RewriteCond %{REQUEST_FILENAME} !-d
Like an if that checks if the url (i.E. domain.tld/home) is NOT a directory because then we want to just access the directory and its index.html or .php ...

RewriteRule ^([a-zA-Z0-9]+)$ /public/$1.php
This is the Rule for what to rewrite.
^ This means that we are rewriting withing this folder (the .htaccess should be in the root directory where the public folder is too)
([a-zA-Z0-9]+)$ This checks wether the part of the url (domain.tld/home) the part here would be home contains nothing else than letters a-z A-Z and numbers 0-9 the + means that the url can have more than one char, the $ signals the end of the "url checker"
/public/$1.php This is the file the url should link too. Because we are in the root folder, we need to get to /public and because the url is domain.tld/home $1 is home so it opens /public/home.php but the url stays domain.tld/home

Hope that helps!

l4zs
  • 103
  • 9