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

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

Related

phalcon shows directory list following the tutorial with wamp server

I've gone through phalcon's documentation and followed the tutorial provided on the official site: https://docs.phalconphp.com/en/latest/reference/tutorial.html
The problem is when I browse the project (localhost/tutorial) it shows me the directory list as
app/
public/
What am I doing wrong?
The .htaccess file for the phalcon directory structure assumes you're working at the web root, not a subdirectory. You'd need to adjust your .htaccess file accordingly if you're not going to follow their proposed directory structure. The .htaccess file they give you is designed to redirect all traffic to the web root to your /public directory thus essentially hiding the /app directory from the web. Try placing their suggested web root .htaccess file in your tutorial directory and add a line RewriteBase /tutorial/. Then place their 2nd .htaccess file in your /tutorial/public directory adjusting its RewriteBase to RewriteBase /tutorial/public/.
Alternatively, you can avoid their public directory altogether and use a safer and faster directory structure, due to avoiding extra RewriteRules, albeit less convenient, by placing your app directory below the web root and adjust your PHP to use either dirname(__DIR__) or .. to refer to the parent directory when attempting to locate your app directory.
A final approach would be to create a subdomain on your localhost, http://tutorial.localhost/, yes localhost subdomains are a great solution for allowing web root access for multiple projects without stepping on each other's toes, then you'd edit your hosts file to add a record for the fake domain mapping it to 127.0.0.1, the same as localhost. Then edit your apache config file adding a virtual host for the subdomain mapping it to a specific directory to use as your web root for that subdomain, reload apache, and presto.
Thanks for the answers...but i figured out the problem myself.
The problem was the configuration setting in apache. I changed the setting in the http.conf in apache folder by removing the # from LoadModule rewrite_module. And that worked for me. cheers!
Steps:
Go to wamp/bin/apache/apacheVersion/conf
open httpd.conf
Uncomment or remove "#" from LoadModule rewrite_module
modules/mod_rewrite.so

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]

How to run PHP application out of localhost?

I got this task form school, to make a PHP web application. But I don't really understand what this requirement might mean
It should be possible to run this application outside the domain root
e.g. sample URL: http://localhost/task/.
I searched a little bit on the internet but was not able to find anything that I could understand ?
I have wamp, and the folder where is my sites is wamp/www/task
When they say "outside of domain root" it means that you should not be forced to go to
http://localhost/yourfile.php
but you could put it in a subdir, like
http://localhost/task/yourfile.php
What they want you to do is harder to guess, but it could mean that you need to be able to run it in any subdir, so take care of you imports to be able to handle that (e.g.: not hardcode the dir you're working in).
Domain root seems to be at localhost, this just means it should be easy to rename your web application folder and make it still work at anywhere.
# http://localhost/task
$ cd wamp/www/
# http://localhost/task2 - should be accessible without you needing to change anything
$ mv task task2
From technical point of view, you should use relative path for all your links and images as well as external resources such as javascript / css files
you can set vitual host for your web server & access your PHP Application likw www.oorja.local
in the wamp server, just add below code at end of your httpd.conf file, which allow you access your PHP application without localhost, Document root and Directory have your physical pathe of your application directory.
ServerName oorja.local
DocumentRoot E:/LAMPSYSTEM/wamp/www/oorja/public
<Directory E:/LAMPSYSTEM/wamp/www/oorja/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>

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

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.

Categories