Relative / Root paths on apache / cpanel / php shared hosting - php

I am trying to deploy a php / codeigniter project to a shared hosting environment.
Locally I am running MAMP and all my paths are referenced thus:-
background:transparent url(/img/myimj.jpg) left top no-repeat;
When I deploy the shared host, these links do not work and to resolve them I need to add "../". Changing all these references alone would be tiresome. but codeigniter paths are also affected and I want to understand how I can have the same mapping as my local instance of MAMP apache.
Not being well versed in apache, I do not know how to resolve this issue. I am using the root public_html folder that has been mapped to my user. Is it possible to use a rewrite rule in a .htaccess to do this?
Thanks for your time.

You could use a .htaccess rewrite rule that just directs all images/css/whatever to a specific directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/relative/to/web-root/
RewriteRule /?([^/]+)\.css css/$1.css
RewriteRule /?([^/]+)\.(jpe?g|png|gif) images/$1.$2
</IfModule>
This is assuming all your images are in a folder called images and all the style-sheets in a folder called css.
In this scenario, it would be best call the images and stylesheets in your code/css using an absolute path. That way images would be cached properly. Even tho the server redirects all the images to the same directory, the client would not see that. So if the same image was called using a relative path from two files in different positions of the tree, the client would see those as two different images and not cache it properly

If your "shared" environment means that you're sharing a DOCUMENT_ROOT, then you'll have to be careful with a .htaccess file - as this will be the .htaccess file for everyone. Otherwise if you have a Virtual host, then what would it take to upload you images into /path/to/document/root/img?

Thanks for your input on this.
Having got through to the web hosting company, I have since realized that the behavior of a virtual host differs if you do not have an ANAME pointing to it. On adding one the folder public_html is mapped as web root as it should be.

Related

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

using laravel in a subdirectory

It seems to me that the idea behind laravel is that the public/ directory is where your DocumentRoot points to and that the app/, vendor/, bootstrap/ and build/ directories all live outside the web root. But what happens if you want the laravel project to live in a subdirectory.
ie. http://www.domain.tld/ might be static HTML, http://www.domain.tld/phpbb/ is phpBB and you wanted your laravel app to live at http://www.domain.tld/app/.
Should you just put the entire laravel folder in http://www.domain.tld/app/ and redirect, via .htaccess, all requests to http://www.domain.tld/app/public/? That seems inelegant.
Maybe you could put laravel in http://www.domain.tld/ and rename the /public/ directory to /app/ but that seems even more inelegant.
Any ideas?
The question is why you need it in subdirectory. So far I never need to put custom project into subdirectory. I use domain or subdomain if needed.
You could try with:
renaming your public directory to the directory name you want to use
put other directories into your root domain directory
add .htaccess for those directories with deny from all (to block access from your root domain)
edit paths in bootstrap/paths.php
I haven't tested it and I wouldn't probably even try to do that. Many frameworks are rather created to use them on separate domains or subdomains and not in directories so I would recommend you to rethink it.
Use virtual hosts in your web engine (eg. apache, nginx, IIS, etc.) and point the document root for your alias to your laravel app.

.htaccess file won't respond on localhost:63342 in PhpStorm

I'm using the IDE PhpStorm 7.1.4 and trying to make an .htaccess file to stop users from going into a specific directory.
My folder structure is like this:
I want to make it so that users can't go in the /app folder or any folders inside that folder. For this, I've figured out that I can use this piece of code inside .htaccess:
Options -Indexes
I'm using the PHP web server from PHPStorm itself (which goes to localhost:63342/projectname/folderinproject/etc/etc/).
Problems
When directing to the page to the /app folder, I get an 404 error,
saying the index file doesn't exist.
When I have made an index.php file inside the /app folder, and I am redirecting to the /app folder, it just loading up the index.php.
When doing this with just a normal HTML project and opening the index.html via my windows explorer, the same problem occurs
Question
How can I make it so that my project would actually respond on the .htaccess file and wont allow me or other users to go into the /app folder?
EDIT
I figured out that when I copy all my files from my project to the c:\xampp\htdocs\ folder and turn on my Apache server inside of XAMPP, the .htaccess file is working whenever I open it via my regular browser (without selecting index.php in PhpStorm and choosing Open in browser...).
Is there any way I can do this same thing in PhpStorm without moving all the files?
If you are using the default configured web server, you are actually using PHP's new web server feature, which doesn't listen to .htaccess files. Only Apache listens to .htaccess files.
If you are wanting to test this functionality, you can either setup a VM running Linux and test, or setup WAMP on your system and run from there.
EDIT 1
Ok, can you add a little more detail about the exact problem? When you access localhost/app/ it is displaying the index.php file, instead of the 404. Does the application work entirely through the index.php file? If so, is the index.php file in the app or public?
EDIT 2
Ok, here's what you need to do. Place an .htaccess file in the root of your app directory. Clear the contents of this .htaccess and place the line DENY from ALL. You can keep the .htaccess file in the root of the project.
EDIT 3
PHPStorm is going to use the PHP Engine's web server. If you add the XAMPP location as a deployment path, it's fairly quick to deploy to. You can even setup PHPStorm to automatically deploy files to the XAMPP location on save. Here's the walk-through on the JetBrains site JetBrains Config.
The .htaccess plugins are mainly for editing and formating, not for modifying PHP Engine's server environment.
Using mod_alias is even easier:
Redirect 301 /app /new_directory
But if you have rewrite rules in your htaccess file already, then you need to stick with using mod_rewrite:
RewriteRule ^app/(.*)$ /new_directory/$1 [L,R=301]

PHP project needs placement in www root-how to fix with .htaccess?

I have to work with several of someone else's PHP projects that have paths hard-coded in such a way that I have to put each project in my /var/www/ directory one at a time to run it - instead of being able to it in its own subdirectory, like /var/www/project_name.
Is there some way to work around this so I don't have to put each project directly in my webroot directory? Having to do that lets me only work with one project at a time with my local LAMP server!
Edit: For doing it the "VirtualHost" way, what would my ServerName be for project_name? I've tried just project_name but that doesn't seem to work.
Add
something like this to your hosts file
localproject1 127.0.0.1
localproject2 127.0.0.1
...
In apache create a virtual server for each of them, each with their own webroot
(and what could be more important) logfiles.
browse to them via http://localproject1, http://localproject2, ...
Basically you have many options, if you want to use different URL's per project you need to edit your hosts file, enable NameVirtualHost and use VirtualHost.
The other way is by placing each project in /var/www/project_name and set the rewrite base in your .htaccess on each folder as follows, so you can use absolute URLs and the project won't break
RewriteEngine On
RewriteBase /var/www/project_name

Set some folder on Apache server as a defaul folder

I'm developing some web based application based on PHP.
I have some folder structure that will be located inside the public html file.
I'd like to make it work so that when a user types for ex. http://mysite.com/ he/she gets into http://mysite.com/public but I don't want the user to know that he/she is inside public, the user should think that his directly inside public_html folder.
Any hints?
P.S. I'm doing it on hosted server, so I have access with only Cpanel, I'm not the admin of the server.
You either need to use mod_alias or mod_rewrite for this. How much of cPanel is available to you? How much does you host let you do?
I'll just have to have a look through my WHM server to work out how to do Aliases, but you can do rewrites with a .htaccess file. I would recommend Aliases over rewrites thought, as they are less complicated and less resource-hungry.
EDIT
Just been into my root login for our WHM/cPanel based server, and I can't find any way to use mod_alias - I think this is probably because it would require an Apache restart. You will have to use mod_rewrite.
Put this in a .htaccess file in public_html:
RewriteEngine on
RewriteRule (.*) public/$1 [L]
You can set up an addon domain and point it to /public_html/public directory.
Edit:
Parked domain should work too.

Categories