Change base HTTP requested directory -- CPanel - php

I have a php websites hosted on a server. I use CPanel to manage it. public_html has lets say following directory structure
public_html
- dir1
- dir2
- dir3
- ....other files.....
- website2home
Now I am trying to make website2home as the base directory of my website, but files inside website2home use some files from public_html folder and some files from within itself.
THE PROBLEM
When I assign a domain name to website2home, It does not reads the files from public_html folder (in fact, public_html folder is not visible) and shows some php warnings and some 404 not found errors. But lets say the domain name for original website is www.aaa.com, then if I access website2home by using www.aaa.com/website2home all works fine.
So My Question...
Is there any way to set the base directory, so that my website2home fetches all files and correct files without replacing hundreds of filepaths in php code?
Please answer considering that I don't want to modify source files of website.

I believe you are looking for some kind of .htaccess configuration. For instance, you may set your website2home folder as base folder for your www.aaa.com domain and then redirect some file access to other folders using .htaccess, something like this..
RewriteEngine on
RewriteRule captcha.jpg ../captcha.jpg
RewriteRule \.pdf$ ../pdf_files/$1
Have a look to mod_rewrite documentation
Edit: If, for some reason, the ../ redirection doesn't work, you always can redirect all your files to a pseudo-index PHP page. For instance:
RewriteEngine on
RewriteRule captcha.jpg my_index.php?access=../captcha.jpg
RewriteRule \.pdf$ my_index.php?access=../pdf_files/$1
And then, in your index PHP page you can simply load your PHP files in other folder:
require "../pdf_files/$requested";

Related

Log all access of subdirectories

I have a directory structure like
/logging.php
/customer1/
/customer2/
/customer3/
...
Now I need to log all access of the subdirs customer1, customer2, customer3 and so on.
Can I somehow (like a htaccess rule) lead all access trough a php file logging.php which saves the access into a mysql database and than forwards to the wanted directory?
I don't want to include the logging.php in every single file of the subdirs, as the subdirs are dynamically generated.
A simple .htaccess rule should suffice.
RewriteRule ^(customer\d+(/.*)?)$ /logging.php?path=$1 [L]
Within logging.php, just look at $_GET['path']
This is assuming that the directory and files in question are in the web root of your site (Usually something like public_html/ or wwwroot/)

Why should we keep index.php in the public folder instead of in the root?

I still don't quite understand why we must keep index.php in a public directory instead of in the root directory.
root/of/project
public/
index.php
.htacess
(html, image, css, etc)
Then, write the following in our virtual host file:
DocumentRoot /path/to/myapp/app/public
<Directory "/path/to/myapp/app/public">
# other setting here
</Directory>
The .htaccess file then redirects all non-existing URLs to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I notice that most frameworks do so, such as Symfony and Zend, just as this tutorial. What is the actual benefits really by having the trouble of modifying the virtual host file?
Why shouldn't we do have this below instead without modifying the virtual host file? Is it a security risk or something?
root/of/project
index.php
.htacess
public/
(html, image, css, etc)
If keeping index.php and modifying the virtual host file is better, how can we modify the virtual host file in the live server then? Let's say I have this domain name, http://my-website.com/ and I access it on my browser, what I see first is not the web page but the directories below untill I click the public directory then I see the home page,
root/of/project
public/
index.php
.htacess
(html, image, css, etc)
I assume that this is the same for Zend or Symfony project as well without being able to modify the virtual host file in the live server. I am no good at these frameworks, I will see this below if I upload my Zend project to the live server,
So, how do you deploy your Zend or Symfony project to your live server to see your web page right away?
It is a very good idea to keep index.php outside of your project root directory because of one simple reason:
You don't want your user to access any files other that one in public folder (index.php, css, js etc). When you will put index.php in root folder you will be also able to access composer.json file for example which is a security risk - a potential hacker will know what packages are you using, in which versions so it's easier for him to perform attack.
When it comes to your hosting - you should have some public_html folder on your root directory which is meant to be public folder (web) of your Symfony app and you should also be able to keep files outside of public folder. If you don't - you really need to think about changing hosting partner
EDIT:
Answering your comment. Let's assume you have public_html folder on your hosting and you want to deploy Symfony app which should be accessible directly on http://your-domain.com. Then you should put whole Symfony project anywhere (but outside of public_html folder) and make a public_html folder a symbolic link to web folder of your Symfony project. This action is equivalent of editing virtual host and changing DocumentRoot which, I assume, you are not able to do.
You can also check my answer on another question to get more clarification

Symfony2 .htaccess should also check if the requested file exists in a subdirectory within web and serve it

I want to extend the following .htaccess rewriteCond to also check if the requested file exists within a subfolder in web (and serve it, not redirect - but serve the file)
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
for example I have the following files in sub-directories within Web folder of symfony project
static/contact.php
static/wiki/countries.php
the request URLs look like this
localhost/symfony/web/contact.php (www.domain.com/contact.php)
localhost/symfony/web/wiki/countries.php (www.domain.com/wiki/countries.php)
i do not want to redirect, just want to serve the contents.
Let me elaborate a little further.
I am currently in the process of upgrading an old php project to symfony2 framework.
The old project has the following URL structure for static pages
www.domain.com/faq.php
www.domain.com/contact.php
www.domain.com/wiki/countries.php
Now in order for this to work as it is - I have to copy the static page and the wiki folder in the WEB folder of the new symfony2 project. Which is fine and the above given Rule / conditions would work and the above URLs would work.
But I do not want to copy the static pages into WEB as it will clutter the WEB folder, and i want to keep it clean - So I moved the static pages to a sub folder within WEB folder called static. Now the above URLs do not work, and throw a page not found error coz all the pages are within web/static folder. so with the current Rule / cond the URLs will have to be changed to
www.domain.com/static/faq.php
www.domain.com/static/contact.php
www.domain.com/static/wiki/countries.php
Is there a way where I can keep the previous URL structure and keep the static pages within a sub folder in web? one way which i can think of is that to have a cond and rule in .htaccesss file within WEB folder to also check for requesting files within static folder and if it is found, then serve them (not redirect)
thanks for you time and help, much appreciated.

.htaccess URL Rewrite to affect Subdirectories

I'm trying to achieve something with .Htaccess RewriteEngine.
I want to be able to controll subfolders via one single .Htaccess file, currently to get this working I have to have the same .Htaccess file in every folder & subfolder but it would be great to just have one in the ROOT folder to avoid copy-pasting.
Currenty my structure looks like this:
ROOT
Images
2012
January
February
...
2013
...
Other
Misc
Folders contain images that are accessed by index.php?i=IMG_NAME and by using the following
RewriteEngine on
RewriteRule ^([^/\.]+)$ index.php?i=$1 [L]
I can use for example: Images/2012/January/IMG_NAME and it works fine to load the image.
But as mentioned I have to have the above .Htaccess file in every folder & subfolder to get it working, otherwise I have to use Images/2012/January/?i=IMG_NAME are there any workarounds for this?
I know this only skirttails the issue but if you don't have the actual url point to a real folder then you can run everything from the .htaccess file from public_html.
So if you actually stored the images in /data/2012/January/*
Then had the url that is used by /Image/2012/January/IMG_NAME

How to route request to codeigniter project outside document root

I have the following shared hosting file structure using a codeigniter project:
myTLD.com/sites/mysite
mysite contains: application, system , index.php ... ( standard CI2 setup )
myTLD.com/public_html - contains : index.php
I have symlinked myTLD.com/public_html/index.php to myTLD.com/sites/mysite/index.php
Unfortunately I am getting:
Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php
I have set it up this way to avoid placing the actual site in the document root for security purposes . I don't want to change mysite/index.php because I want to keep the entire project in its mysite directory where it can easily be revised etc.
The application and mysite/ folder are set to 755 so I don't think this is a permission problem .
My myTLD.com/public_html/.htaccess folder directs all requests to index.php:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Can someone advise me on an approach to sending requests through to the codeigniter index file without causing this error?
Thank you
You can try following way
1) Remove the symlink
2) Use this in htaccess at myTLD.com/public_html/.htaccess
RewriteEngine on
RewriteRule ^(.*)$ myTLD.com/sites/mysite/index.php?/$1 [L]
Use absolute system path if you are aware of it.

Categories