0

I am coding a website with php, html, css and a few javascript. I am on localhost, more exactly LAMP server, and for me to access this "website" the url on the browser is http://localhost/website.

What I want is to have an subfolder that has a index.php. So I had made an index.php on the main folder that includes the subfolder/index.php and everything works great.

Now I want to create an about page on the subfolder (subfolder/about.php). However the url should be localhost/website/about.php and this doesn't work.

How can I have a subfolder like it was the main folder to display?

trying to simplify the question:

website/
    subfolder/
        index.php
        about.php
        products.php
        etc.php
    index.php

imaging that this above is the files directory, probably its possible to understand.

so the link should be website/about.php and not website/subfolder/about.php basically what I need is that the subfolder acts like it was the root folder of the website.

tiago calado
  • 156
  • 7
  • You got a response. maybe error etc.? What means doesn't work? – Maik Lowrey Dec 11 '21 at 17:01
  • `website/about.php >>> – Jared Farrish Dec 11 '21 at 17:03
  • It's difficult to understand what you are asking for. Is it possible you are looking for https://www.php.net/manual/en/function.set-include-path.php or https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base – v010dya Dec 11 '21 at 17:06
  • I would like the subfolder act like it was the main domain. any website/anypage.php should work like that and not webset/subfolder/anypage.php – tiago calado Dec 11 '21 at 17:06
  • It's a goog idea to build your web defining a base path and a base URI. – José Carlos PHP Dec 11 '21 at 17:11
  • "this doesn't work" - what does that mean? Please share more details – Nico Haase Dec 11 '21 at 17:16
  • basically all I want is to code files in the subfolder and display them in the url website/anyfile.php and not website/subfolder/anyfile.php – tiago calado Dec 11 '21 at 17:26
  • 1
    Rather than duplicating your efforts, or writing a front controller, or using other rewrite hacks: I personally find it's just easier to redefine your document root. If you are on a *nix machine, you could try a symlink. Instead of a folder for your document root, you can link there. Or the subfolder etc. I tend to do `/var/www -> /path/to/project/www` for a simple development environment switch. – Progrock Dec 11 '21 at 18:13
  • If you have an asset included within your `about.php`, like an image or javascript, note that your links may break by this approach. – Progrock Dec 13 '21 at 11:08

2 Answers2

1

Simple front contoller:

website/index.php

<?php

switch ($_GET['page'] ?: 'index')
{
    case 'about':
        require 'website/subfolder/about.php';
    break;
    case 'products':
        require 'website/subfolder/products.php';
    break;
    default: // index
        require 'website/subfolder/index.php';
}

Then setup a front controller rewrite using an .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /website/index.php?page=$1 [QSA,L]

https://stackoverflow.com/a/26755631/451969

Now urls like these should indirect to your website/index.php front controller as a page GET variable:

website             # index
website/about       # about page
website/products    # products listing
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • I am using a LAMP server. Should this .htaccess be in the website/ directory – tiago calado Dec 11 '21 at 17:23
  • @tiagocalado I think so; you might have to tweak it a little. For instance, you might need to remove the `/website` part if it's in the website folder. I don't mess with `RewriteRule` very often... – Jared Farrish Dec 11 '21 at 17:25
  • This seems to be a good solution, redirect any url to the index.php and from there make the distribution, I can work with this. however in my localhost It seems the .htaccess is not being apply, maybe this is in my system, I am using a LAMP configuration? – tiago calado Dec 11 '21 at 17:31
  • @tiagocalado Also, I'd recommend putting those PHP files out of the web path so they can't be accessed directly with http. Somewhere below the web root. An alternative is to add some code to force redirect if that doesn't happen (e.g. test if a constant you define in `website/index.php` is set and if not, redirect to it's pure route). – Jared Farrish Dec 11 '21 at 17:31
  • @tiagocalado You should be able to use `.htaccess` if you have Apache. Unfortunately I don't have a lot of experience debugging Apache scripts. – Jared Farrish Dec 11 '21 at 17:34
  • thanks for the info, this makes sense to me, I believe its just on my system that probably isnt working for some reason, must figure it this out. – tiago calado Dec 11 '21 at 17:38
  • @tiagocalado I won't be able to right away, but I'll see if I can create a repo that works as described. – Jared Farrish Dec 11 '21 at 17:53
0

just use this code

<?php
  $uri = urldecode(
 parse_url($_SERVER['REQUEST_URI'], 
 PHP_URL_PATH)
 );

 if ($uri !== '/' && 
 file_exists(__DIR__.'/subfolder'.$uri)) {
 return false;
 }

 require_once 
  __DIR__.'/subfolder/index.php';
ganesh rao
  • 52
  • 1
  • 8
  • it works for the index.php subfolder, then I have there about , this opens website/about.php with 404 error code, and I have the about.php but in the subfolder – tiago calado Dec 11 '21 at 17:14
  • Please add some explanation to your answer such that others can learn from it. How does it include `about.php` from the subfolder? Also, how do you ensure that this does not create security issues where you could include any file from the system? – Nico Haase Dec 11 '21 at 17:17
  • This is an incomplete answer. You check that a file exists, but do nothing with it. – Progrock Dec 13 '21 at 11:06
  • @tiagocalado you can easily create functions or routes to go to a particular page for example in place of index.php just get the url and store its value in a vairable then use it in place of index.php simple – ganesh rao Dec 21 '21 at 07:26