Search code examples
php.htaccessdirectorysubdirectory

Send all Request to Inside folder in php?


sharedHostingFolder

folder1

folder2

folder3

xampp htdocs folder

Folder Structure:

root/sharedHostingFolder
-need an .htaccess file here
-folder1
        --index.php
        --dude.php
-folder2
        --hello.php
        --index.php
        ---folder5
                       ----index.php
-folder3
        --hi.php
        --index.php

Request Structure

Request "root/sharedHostingFolder/hello.php"         will show file inside   "root/sharedHostingFolder/folder2/hello.php"

Request "root/sharedHostingFolder/"                  will show file inside   "root/sharedHostingFolder/folder2/index.php"

Request "root/sharedHostingFolder/folder1/dude.php"  will show file inside   "root/sharedHostingFolder/folder1/dude.php"

Request "root/sharedHostingFolder/folder1/"          will show file inside   "root/sharedHostingFolder/folder1/index.php"

Request "root/sharedHostingFolder/folder2/hello.php" will show file inside   "root/sharedHostingFolder/folder2/hello.php"

Request "root/sharedHostingFolder/folder2/"          will show file inside   "root/sharedHostingFolder/folder2/index.php"

Request "root/sharedHostingFolder/folder3/hi.php"    will show file inside   "root/sharedHostingFolder/folder3/hi.php"

Request "root/sharedHostingFolder/folder5/"          will show file inside   "root/sharedHostingFolder/folder2/folder5/index.php"


I HAVE NO ACCESS TO php.ini file

.htaccess file

RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_URI} ^/folder1/ [OR]
RewriteCond %{REQUEST_URI} ^/folder2/ [OR]
RewriteCond %{REQUEST_URI} ^/folder3/
RewriteRule - [END]
RewriteRule ^ /folder2%{REQUEST_URI} [END]

httpd.conf file:

<Directory />
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>



Error:

object not found

I researched and found out .htaccess will do that easily . But, Really I don't know how to do that.


Solution

  • This is a strange question. TO me it reads a bit like a classical xy problem... As if you did not ask how to solve your actual task, but how to get something to work that you think might solve your task...

    Anyway, looking at your examples I have the impression that little is to be done, mostly some exceptions that need to be implemented as rewriting rules. So something like that:

    RewriteEngine on
    DirectoryIndex index.php
    
    RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder1/ [OR]
    RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder2/ [OR]
    RewriteCond %{REQUEST_URI} ^/sharedHostingFolder/folder3/
    RewriteRule - [END]
    
    RewriteRule ^ /sharedHostingFolder/folder2%{REQUEST_URI} [END]
    

    This leaves requests to URIs starting with one of the folder names untouched, replying on classical file system mapping and defining the index.php script as fallback logic for those folders. And it maps everything else into folder2.

    It does not get clear from your question what that "www" string is meant to represent. If that is meant to represent another folder level then you might have to add that level to the rules above.

    For this to work the rewriting module needs to be enabled, obviously. Best is to implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess"), but you need to enable the interpretation of such file for the requested location (see the apache documentation about the AllowOverride directive).