How to set root directory to a subfolder (local php development environment) - apache

I'm just starting to learn web dev, but got myself a bit confused when setting up my local development environment.
I had a php site on shared hosting for some time. I would edit local files and upload them, then refresh to ensure everything worked. A naughty practice I know!
So I have tried to set up a local dev environment and installed AMPPS for a local webserver (PHP5).
I have ambitiously set up the folder structure below so that I can code up a range of websites to try out different languages and frameworks whilst keeping them all seperate. (Square brackets denote folders). Note sure if this is the correct approach... (?)
-->[PHP]
---> [AMPPS]
----> [www]
-----> [PROJECTS]
------> [project_name]
-------> index.php
-------> .htaccess
-------> [images]
-------> [section1]
--------> page1.php
-------> [etc...]
------> [project_name]
------> [project_name]
--> [RUBY]
--> [PYTHON]
My first hurdle however is that all of my links were relative, and I had of course uploaded my site at root with my host. Root in my local dev enironment is now www, which as you can see is several folders up from where I have installed my site.
Links (e.g. Page 1) now display as: "localhost/section1/page1.php" when I believe they should in fact show as "localhost/PROJECTS/project_name/section1/page1.php".
I don't think that telling APACHE that my root directory is [project_name] is the correct approach, because then it wont work for other projects I create. I assume that I should be doing something on a project by project basis to specify it's own root directory.
I thought that I could specify root by adding an .htaccess file to the project_name folder:
From my online research I thought it would go something like:
RewriteEngine on
RewriteRule ^/$ /PROJECTS/project_name/
or perhaps
RewriteEngine on
RewriteRule ^/$ .;C/PHP/AMPSS/www/PROJECTS/project_name/
However I can't get these to work.
When the site was online, I had also coded <?php set_include_path($_SERVER['DOCUMENT_ROOT']); //Look for includes starting from ROOT location ?> at the top every page, thinking I was doing the right thing.
Now I am not so sure that I correctly understood what I was doing there. It doesn't seem to have any impact with or without it in my development environment. And I haven't been able to edit it in any way successfully to resolve my relative links issue.
Sorry for the long post.
I'm grateful for any and all feedback/assistance. Thanks in advance :)
[Resolved] - The advice below was perfect. Thank you all. :)
A tip for Chrome users:
If you are using a custom domain, like I did with .dev, either proceed the address with http://, or end it with a trailing /.
For example I now visit my dev site in Chrome by visiting projectname.dev/ (without the last / it tries to search).
At first I thought I had set up VirtualHost or hosts incorrectly, when in reality it was this Chrome behaviour that that was clouding the issue. At the time of writing, other browsers like Firefox don't share this concern.
Hope that helps someone else out.

You're correct that configuring Apache for each project is the way forward. It's best done with virtual hosts. Define a virtual host for each project and give it a domain name or subdomain for development. In my case I use test.com for all my local development and configure project1.test.com, project2.test.com etc so all my relative paths work as expected. For this I also edit my hosts file so the DNS lookup for test.com and sub projects resolve to my local machine.
Rewriting URLs is something I reserve for a project-by-project basis, without setting global rewrites for a specific server setup. That way my local development environment is close to the actual server environment and project-specific rewrites will work as expected.
Hope that helps

I recommend using method mentioned by #Tak, but I know there might be problems with pre-configured WAMP packs to add virtual hosts, so there is also a solution to use BASE tag in head section of your html:
<base href="http://localhost/PROJECTS/project_name/">

Related

Correct Apache Configuration And Htaccess

I've just reset my Ubuntu 14.04 LAMP server hosted with digital ocean. Could someone tell me the 'proper' way to do server configuration. My goal is to do everything as clean as possible (and hopefully well structured).
I intend on using the server mainly for programming and data analytics, however I do plan on hosting my website in /var/www/html. I also plan on using letsencrypt/certbot to get an easy SSL. With this in mind, these are the main goals I would like to accomplish:
1) Redirect the website to ALWAYS be served through https AND www.
2) Enable HSTS for the entire website.
3) Enable clean url's (remove .php extensions and what not).
Since I would like all of these properties to be used across the entire website, should the configuration be done inside of the /etc/apache2/ folder? Or should it be done inside of .htaccess?
And if it should be done inside of apache2 configuration, which file should I add it to? And finally, how exactly should it be added? (for example vhost 80/443, inside of a mod_something section, etc).
Thank you in advance, I would appreciate and consider any advice about Apache and htaccess!

Contao is redirecting to non existend development subdirectory

Im currently trying to move a website to production that has been designed using contao. The website has been developed in a subdirectory of the current website. Now when I change the DocumentRoot to the contao directory the application automatically forwards me to the /development directory which in that situation does not exist anymore.
I did a database dump and searched it for "/development" to find the variable that is storing this information but I didn't find a match. I then did the same with the configuration files but it didn't show up any results too. How is redirecting handeled in contao?
It sounds like your pathconfig.php is wrong. After deploying/moving a Contao installation, you should always execute and log into the Install Tool under contao/install.php. This will correct your pathconfig.php.
Also make sure the RewriteBase in the .htaccess is set to the correct value (probably / in your case).

htaccess - simulating local page as web page

Is there a way to rewrite a URL in a local project to look like web page?
For example I have project with url
localhost/site
I'm tryin' to rewirite this to:
www.site.com
or
site.com
That project has subpages, and it would be good if it worked like
site.com/subpage.php
I'm trying for an hour but I'm really htaccess noob.
I work with VertrigoServ and mod_rewrite works fine with some examples which I tried in other projects.
The issue is not with rewrite. To make a local page appear to have a more conventional domain you need to tell your browser that the conventional domain is found on your local webserver.
The simplest way to do this is by editing the hosts file on your OS. On *nix based devices it's usually /etc/hosts, On windows it's usually C:\Windows\System32\drivers\etc\hosts. You will need elevated privileges to edit the file.
Add a line to the end of the file that maps the domain name to your local ip address like this:
127.0.0.1 www.site.com
Close an reopen your browser and visit www.site.com, you should see it is loading the page from your local webserver.
Chances are, you are still seeing the same page as if you view localhost site. To make it load your code you need to change the DocumentRoot in httpd.conf for your apache install to match the directory where your code is.
A preferable solution may be to use Name Based Virtual Hosting, this allows multiple web sites to share the same IP address. Searching 'apache VirtualHost examples' should give you plenty of resources for this. Make sure that NameVirtualHost *:80 is also enabled in order for this to work.

Aliases on Dreamhost, general management of http request / server errors

I had a hard time deciding how I should manage these errors (404, 500, ...) and when I finally decided, I am encountering problems. This is a reeeeeally long question, I appreciate anyone's attempt to help!
Let me first describe how I decided to set it up. I have several sites hosted on a shared Dreamhost account. In the folder structure that I see, everything of mine on the server is under /home/username, and for example, site1.com's web root is at /home/username/site1.com
I am creating a generic error handler (php script) for errors like 404 not found, 500, etc. that I want to store above the web roots of my sites at /home/username/error_handler/index.php so that I can use an .htaccess file at /home/username/.htaccess which includes something like the following:
ErrorDocument 404 /error_handler/index.php
ErrorDocument 500 /error_handler/index.php
...and many more
When these errors occur on any of my sites, I want it to be directed to /home/username/error_handler/index.phpThis is the problem I'm having a hard time figuring out. The ErrorDocument directives above will actually cause Apache to look for /home/username/site1.com/error_handler/index.php
Anyway, the errors should be redirected to my error handling php script. The script will use $_SERVER['REDIRECT_STATUS'] to get the error code, then use $_SERVER['REDIRECT_URL'] and $_SERVER['HTTP_HOST'] to decide what to do. It will check if an error handler specific to that site exists (for example: site1.com/errors/404.php). If this custom page doesn't exist, it will output a generic message that is slightly more user-friendly and styled, and perhaps will include some contact info for me depending on the error.
Doing it this way lets me funnel all these errors through this 1 php script. I can log the errors however I like or send email notifications if I want. It also lets me set up the ErrorDocument Apache directives once for all my sites instead of having to do it for every site. It will also continue to work without modification when I move the site around, since I already have a system that scans the folder structure to figure out where my site roots are when they really aren't at the web root technically speaking. This may not be possible with other solutions like using mod_rewrite for all 404 problems, which I know is common. Or if it is possible, it may be very difficult to do. Plus, I have already done that work, so it will be easy for me to adapt.
When I am working on sites for which I don't have a domain name yet (or sites where the domain name is already in use at the moment), I store them temporarily in site1.com/dev/site3.com for example. Moving the site to site3.com eventually would cause me to have to update the htaccess files if I had one for each site. Changing the domain name would do the same.
Ex: a site stored at site1.com/dev/site3.com would have this in its htaccess file:
ErrorDocument 404 /site1.com/dev/site3.com/error/404.php
And it would have to be changed to this:
ErrorDocument 404 /site3.com/error/404.php
Obviously, this isn't a huge amount of work, but I already manage a lot of sites and I will probably be making more every year, 95% of which will be hosted on my shared DreamHost account. And most of them get moved at least once. So setting up something automatic will save me a some effort in the long run.
I already have a system set up for managing site-relative links on all my sites. These links will work whether the site exists in a subdirectory of an existing site, or in their own domain. They also work without change in a local development server despite a difference in the web root location. For example, on the live server, the site-relative http link /img/1.jpg would resolve to the file /home/username/site1.com/img/1.jpg while on my local development server it would resolve to C:\xampp\htdocs\img\1.jpg, despite what I consider the logical site root being at C:\xampp\htdocs\site1.com. I love this system, and it is what gave me the idea to set up something that would work automatically like I expected it to, based on the file structure I used.
So, if I could get it to work, I think this seems like a pretty good system. But I am still very new to apache configuration, mod_rewrite, etc. It's possible there is a much easier and better way to do this. If you know of one, please let me know.
Anyway, all that aside, I can't get it working. The easiest thing would be if I could have the ErrorDocument directive send the requests to folders above the web root. But the path is a URL path relative to the document root. Using the following in /home/username/.htaccess,
ErrorDocument 404 /error_handler/index.php
a request for a non-existent resource causes Apache to look for the file at
site1.com/error_handler/index.php
So I thought I should set up a redirection (on all my sites) that would redirect those URLS to /home/username/error_handler. I tried a few things and couldn't get any of them to work.
Alias seemed like the simplest solution, but it is something that has to be set at server runtime (not sure if that is the right terminology - when the server is started). On my local server, it worked fine using:
Alias /error_handler C:\xampp\htdocs\error_handler2
I changed the local folder to test that the Alias was functioning properly. (On the local server, the URL path specified by the ErrorDocument directive is actually pointing to the right folder, since in my local server the web root is technically C:\xampp\htdocs and I store the error handler I want to use is stored locally at C:\xampp\htdocs\error_handler\index.php)
Dreamhost has a web client that can create what I am guessing is an Alias. When I tried to redirect the folder error_handler on site1.com to /home/username/error_handler, it would seem to work right if I typed site1.com/error_handler in the browser. But if I typed site1.com/test1234 (non-existant), it would say there was a 404 error trying to use the error handler. Also, I would have to login through the web client and point and click (and wait several minutes for the server to restart) every time I wanted to set this up for a new site, even if I could get it to work.
So I tried getting it to work with mod_rewrite, which seems like the most flexible solution. My first attempt looked something like this (stored in /home/username/site1.com/.htaccess for now, though it would eventually be at /home/username/.htaccess:
RewriteEngine On
RewriteRule ^error_handler/index.php$ /home/username/error_handler/index.php
The plain english version of what I was trying to do above is to send requests on any of my sites for error_handler/index.php to /home/username/error_handler/index.php. The mis-understanding I had is that the subsitution will be treated as a file path if it exists. But I missed that the documentation says "(or, in the case of using rewrites in a .htaccess file, relative to your document root)". So instead of rewriting to /home/username/error_handler/index.php, it's actually trying to rewrite to /home/username/site1.com/home/username/error_handler/index.php.
I tried including Options +FollowSymLinks because in the Apache documentation it says this:
To enable the rewrite engine in this context [per-directory re-writes in htaccess], you need to set "RewriteEngine On" and "Options FollowSymLinks" must be enabled. If your administrator has disabled override of FollowSymLinks for a user's directory, then you cannot use the rewrite engine. This restriction is required for security reasons.
I searched around for a while and I couldn't find anything about how Dreamhost handles this (probably because I don't know where to look).
I experimented with RewriteBase because in the Apache documentation it says this:
"This directive is required when you use a relative path in a substitution in per-directory (htaccess) context unless either of the following conditions are true:
The original request, and the substitution, are underneath the DocumentRoot (as opposed to reachable by other means, such as Alias)."
Since this is supposed to be a URL path, in my case it should be RewriteBase /, since all my redirects will be from site1.com/error_handler. I also tried Rewrite Base /home/username and RewriteRule ^error_handler/index.php$ error_handler/index.php. However, the Rewrite Base is a URL path relative to the document root. So I need to use something like an alias still. The implication in the quote from the documentation above is that it is possible to use mod_rewrite to send content above the web root. One of the many things I don't know is what the 'other means' besides Alias might be. I believe Alias might not be an option on Dreamhost. At least I couldn't make sense of it.
Why not use error pages in the site root, then include the actual file from the shared section?

Problem in apache2 with mod rewrite when setting rules in .conf files instead of .htaccess

Because of weird security policies of my hosting provider I have to define my rewrite rules in /etc/apache2/conf.d/examplesite.conf instead of writing them on an .htaccess on the www folder of that site.
What I'm trying to do is setup a Wordpress Mu server (http://mu.wordpress.org/forums/topic/17349 ) and so far its working on a 50%.
The main blog loads perfectly but other sub blogs (located for example at www.example.com/blog2 ) don't.
I'm guessing the problem is that the rewrite rules behave differently when declared at .conf files for each virtual host instead of using .htaccess files.
Has anybody else had this problem? How can you fix it?
This doesn't sound like a rewrite problem to me but maybe it is. You don't say what the error is when you try to load one of the sub blogs. Perhaps posting up what your rewrite rule is would be helpful. Also would you be able to set up a scenario where you did them in .htaccess files on a localhost or something and seeing if there was a difference?
If pretty permalinks work, then mod_rewrite is enabled, and rewriting URLs to WordPress successfully.
If this is the case, then it's a problem with your MU install.
Did you choose paths over sub-domains during the MU install? If you didn't, but then later switched, that's where the problem is - are you in a position to fresh install?