Redirect to hid subdirectory


How can I hide the subdirectory so that the pages appear to be on root?

Two scenarios
I have my cms platform (whatever) in its subdirectory i.e. cms
and want to hide cms in browser address bar, and this for all pages of cms, so that when user types goes directly to without seeing cms, thus navigate thru it without eve knowing they are in a subdirectory
e.g. > >,php >

I have different cms (whatever), and an index file on root, so the user gets to when typing and from there can choose where to go
two link in index: “gallery” (goes to photogallery cms) “articles” (goes to blog cms)
so they should see this > <<< gallery removed and gallery index.php file shown > <<< gallery removed > <<< blog removed and blog index.php file shown > <<< blog removed

Thank you

You can use Apache with mod_rewirte
put .htaccess in your documents root with something like that inside:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cms/$1 [L]

then will actually open (but only if qwe.php doesn’t exist in the root)

Thank you.

I guess then it’s not possible since there is in both cases at least the index.php present, right?

I use a similar script that @megazoid supplied to test for files in a cache folder. If the file exists then it is activated otherwise the condition fails and falls through to the next test.

I think if you test the script it will be OK because .htaccess is called first.

You may have to amend the trailing [ L] parameter to quit parsing the reminder of the .htaccess file.

Tapped laboriously from a mobile and not able to test at the moment.

Remove that string:

RewriteCond %{REQUEST_FILENAME} !-f

and it will not check if file exists

This method may satisfy all your conditions:

I have a _CACHE-FOLDER that I test first to see if the cached file exists.
If it does then a .html version is activated otherwise the rule fails and falls through to /index.php


DirectoryIndex index.php  index.html  index.htm 

RewriteEngine On
RewriteRule ^(.+)$ /___CACHE-FOLDER/$1.html [QSA,L]

Laravel, generate and renders content (which is saved to the CACHE-FOLDER - for future reference).

N…B File extensions are not used or displayed in the “browser address bar”.

Thank you both.

I’ve tried both suggestions, and it doesn’t seem to work, I still see my directory:

I assumed I have to change cms with my directory, and did so

RewriteRule ^(.*)$ /mydirectory/$1 [L]

and same with CACHE-FOLDER which I changed to mydirectory, to not let anything left, I also used your suggestions as you posted.

How did you test it?

Thank you for your patience.

I loaded a subdirectory with its own index.php, then created a link in index.php in the root, clicked on the link, and it goes to the subdirectory, but shows the subdirectory name in browser address bar.

I tried .htaccess both in root, and in subdirectory.

Where did that link point to?
If u make direct link to /subfolder/index.php then of course you will see /subfolder/ in URL

RewriteRule ^(.*)$ /mydirectory/$1 [L]

That rule means: “if there is something in URL, then load and show that from /mydirectory/”
but not “hide /mydirectory/ in URL”

For example:
URL = /qwe.php, file loaded in browser = /mydirectory/qwe.php
URL = /mydirectory/qwe.php, file loaded in browser = /mydirectory/qwe.php

And that also means you should have only one index.php (in subfolder, but not in root), otherwise how the server will know what to open at root?

Thank you.

I had understood that, but not the direct linking, how am I going to load the pages in subdirectory then?
Hiding the subdirectory is my goal, but seems from your explanation that’s not technically possible, so I guess I have to think over how to go about structuring the site.

Links to that pages should not contain /subdirectory/

It depends on your CMS. If there is a way to change all links by config (remove /subdirectory/), then it’s possible.

Thank you.

I haven’t decided on the cms yet, question was to see first the possibilities before making the decision, you clarified the situation; thanks again.

I believe most CMS and PHP Frameworks entry point is always /index.php. This server file is then parsed and render a HTML, CSS and JavaScript file to a main/root menu.

All URLs first pass through the server index.php and then directed to an internal router where the relevant URL data is extracted and then sent to the CMS/PHP Framework “Engine”. An excellent Application Flow Chart can be seen here:

I think most CMS/PHP Frameworks supply an optional .htaccess file can hide /index.php. Configuration file parameters may be finely tuned to completely remove .php extensions or to add the old standard .html file extension. Modern trend is to have no file extensions. Pretty URLs may also be configured to eliminate potential security risks when using $_GET parameters.

Most CMS/PHP Frameworks have caching available which automatically stores the the rendered web page. As you can see from the diagram, the routed URL first tests for the existence of a cached file and if it exists the cached file is rendered without loading a any Drivers, Models, Libraries, Helpers, Packages or Scripts.

Other advantages of using a single /index.php is that system updates are activated by setting the index.php->$system_path and if there if there are any problems then quickly revert back to the previous version. Online Development versions can run in tandem and eliminates ancient “Under Construction” pages.

Thank you John.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.