views:

63

answers:

2

Hi all,

I have a .htaccess file & I currently I am working on localhost. For a 404 page error, I have the following code in the .htaccess file:

ErrorDocument 404 /my_local_domain/404.php

But when I upload this file to my website online, the functionality of the file breaks. It no longer shows the 404.php page. It works if I modify the code in the .htaccess file of my online website to the following:

ErrorDocument 404 /404.php

Now all through the changes that I do in the .htaccess file, I would have to remember to remove the domain name before I upload it to the website or I risk breaking the functionality. So with this in mind, here are my questions:

1. How do I solve the above problem without needing to edit the .htaccess file each time (by stripping it off the my_local_domain) I make a change & upload it online?

2. How do I setup 404 page redirection for all the nested folders? (I don't want to setup a .htaccess file for each of the folders. A single .htaccess file that resides in the root folder of the website & controls all the redirection for all the sub-folders would be awesome)

All help is appreciated.

Thank you.

+1  A: 

First of all I suggest you setup local domains for development. For example if you are developing a website which will go under www.example.com, you can setup a local.example.com in your HOSTS file. You'll do a VirtualHost setup in your apache and the .htaccess will then be the same.

Also, you can setup a build process (e.g via Ant) which will allow you to prepare and generate a zip file with the files which go on the live server. This build will feature the correct configuration files (db configs, mail servers, htaccess etc).

cherouvim
@cherouvim Thank you for the reply. In the code `ErrorDocument 404 /my_local_domain/404.php`, `my_local_domain` is the local domain. I just renamed it for illustration purposes. So how should I modify the .htaccess file now?
Devner
It seems that you refer to a relative folder which you call my_local_domain. If you are developing on http://local.example.com/ and deploying on http://www.example.com/ then in both cases the htaccess directive will be ErrorDocument 404 /404.php
cherouvim
Devner
@Devner: The concept is that you'll set up your dev environment so that .htaccess files will be identical between the dev and live environments.
cherouvim
@cherouvim Thanks for the reply. My folder structure is like: wamp/www/site1/, wamp/www/site2/...wamp/www/site10/. So given this, what can I do to fix it?
Devner
Set VirtualHost sections which will be serving each site in a separate domain. A domain only known to your computer using the HOSTS file. For example site1.devner.com, site2.devner.com etc
cherouvim
With the current setup that I have, I am finding it hard to understand how to make this possible as I am afraid this will mix-up all the existing sites that I have setup on my localhost. So how do I go about this?
Devner
You can set as many VirtualHosts as you like. It's no problem.
cherouvim
So in my case, do I add `127.0.0.1 localhost site1`, `127.0.0.1 localhost site2`,... `127.0.0.1 localhost site10` in my hosts file to be able to show the site1, site2 ...site10 domains by using only 1 .htaccess file? Am I right?
Devner
Just add `127.0.0.1 site1` `127.0.0.2 site2` etc. Then you'll configure the apache VirtualHosts
cherouvim
Devner
+2  A: 

I believe you have two different issues here.

First of all, you should not need to have different paths in development and live site. It appears that you've configured your local Apache to host only one site and each actual sites goes in a subdirectory. It's not a good idea: you'll soon be mixing cookies and sessions between all your dev sites. Have a look at the name based virtual hosts feature: you can configure as many independent sites as you need. You don't even have to buy real domains in you set them in the hosts file.

Secondly, under certain circumstances it can be useful to have different Apache directives. I've been using the following trick.

  1. Pick a keyword for the dev server, e.g. DEV_BOX.
  2. Pass that keyword to Apache in the -D parameter. If you run it as service, you can run regedit and find the HKLM\SYSTEM\CurrentControlSet\Services\Apache2.2\Parameters key. Append -D DEV_BOX to the ConfigArgs value. Restart Apache.
  3. Now, you can use the <IfDefine> directive to set local directives:

-

#
# Common stuff
#
AddDefaultCharset UTF-8


#
# Local-only stuff
#
<IfDefine DEV_BOX>
    Options +Indexes
</IfDefine>


#
# Live-only stuff
#
<IfDefine !DEV_BOX>
    Options -Indexes
</IfDefine>
Álvaro G. Vicario
Nice hack with the IfDefine, but I believe that production and development should feature different configuration files. The build system is responsible for bundling the correct files and generating the distrutable.
cherouvim
@Alvaro, Thanks for the reply. Yes, I have my folder structure like: `wamp/www/site1/`, `wamp/www/site2/`...`wamp/www/site10/`. So given this, what can I do to fix it?
Devner
Read the link I provided and create one different web site for each project instead of having one common web site for all projects.
Álvaro G. Vicario
@Alvaro, so do you mean that I should create `wamp/www1/site1/`, wamp/www2/site2/...etc.? Is this correct?
Devner
@Devner, don't let Wamp decide your layout. Put your site files wherever you normally put your stuff, e.g. `C:\Projects\SiteFoo`, `C:\Projects\SiteBar`. Then create some virtual hosts in Apache and point them to your directories with the `DocumentRoot` directives.
Álvaro G. Vicario
@Alvaro Thanks for letting me know but I am unsure of how to create virtual hosts. I did create sites like: wamp/www/site1/, wamp/www/site2/...wamp/www/site10/. So given this, what can I do?
Devner