I was decommissioning an old server the other day, and in the process of moving the system to a new server I learned that the old domain was in fact one that we didn’t directly control. In other words, time to enforce a new domain on our clients! Of course, getting anyone to change URLs or domains on their personal machines is never an easy thing, so of course there has to be a small period of playing nice, in which you need to seamlessly redirect from one domain to the other. In other words, if an old URL is oldserver.com/about/ then I want anyone hitting that URL to be redirected to the new URL at newserver.com/about/.
To do this on your LAMP server is pretty easy thanks to the powerful mod_rewrite Apache module. Simply open up your relevant vhost declaration file in /etc/apache2/sites-available and add the following to the bottom of it (obviously using your own domains in place of the sample ones):
If you have a folder which you need to access in the standard way without your Zend Controller handling it for you (maybe a direct file download outside of your Zend web application), the easiest and quickest way to do this is to simply create a .htaccess file inside the folder you want your Zend controller to ignore and populate it with:
RewriteEngine Off
Because .htaccess files work in a cascading manner, this will overwrite the Zend controller .htaccess file in the root folder, in essence turning off mod_rewrite and thus giving you full control over the folder in terms of Apache now serving it as how it normally would.
Quick and easy.
(There are of course loads of other ways to do this, like implementing something like “RewriteRule ^(admin|user)($|/) – [L]” – if you want to ignore folders admin or user – at the top of your main Zend .htaccess file in root, but I like the above as a particularly quick and easy way to simply ignore a specific directory!)
If your freshly installed WordPress site is throwing standard 404 errors (the Apache default look and feel 404 pages mind you) instead of showing your content when accessing via permalinks, then chances are pretty pretty high that either a) WordPress can’t write to your .htaccess file or b) the Apache rewrite (mod_rewrite) module isn’t installed.
To solve this you’ll need to have SSH access into your Ubuntu server. Once you have logged in, head over to your WordPress install directory and see if there is a .htaccess file there:
ls -a .htaccess
If it isn’t there then that means that WordPress was unable to automatically create it, probably because of file rights. To resolve this, create a .htaccess file:
touch .htaccess
And populate it with the following WordPress default .htaccess content:
Finally, let us go make sure that the Apache mod_rewrite module is installed by running the following:
sudo a2enmod rewrite
sudo service apache2 restart
With the mod_rewrite now enabled under Apache, next is to ensure that the AllowOverride directive is set appropriately for your Apache vhost directory declaration – you can use either AllowOverride FileInfo or AllowOverride All to achieve this. For example, for the default website, edit /etc/apache2/sites-available/default to reflect:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
# changed from None to FileInfo
AllowOverride FileInfo
Order allow,deny
allow from all
</Directory>
With both of these now in place, your permalink problem should be solved.
Unfortunately domain names are seldom cast in stone when it comes to commercial products, because as with most things, people tend to change their minds further down the line.
If for the last three years your web application has been running under the domain myapp.com but now the boss has decided that it should rather be running under myawesomeapp.com, here is but one method that can help ease your pain in changing the domain of your site.
For this to work, we are assuming that you are running an Apache webserver which has the mod_alias module activated.
The first step would be to create a new virtualhost for the new domain (myawesomeapp.com) under the sites-available apache2 directory and enable it using the handy a2ensite script (if you are running Ubuntu Linux).
Next you would open up your existing site entry for myapp.com and change its contents as follows:
Save your changes and run the following in order to reload Apache’s configuration:
sudo /etc/init.d/apache2 reload
Breaking down what you’ve just done, basically you’ve instructed that for every request coming in to myapp.com, the server should redirect (with a permanent marker) to the new myawesomeapp.com domain but with the rest of the request string intact.
What the documentation tells us: This directive is equivalent to Redirect, but makes use of regular expressions, instead of simple prefix matching. The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into the given string and use it as a filename.
The extremely useful mod_rewrite or rewrite module is not enabled by default on a new Apache web server install. Luckily, this turns out to be pretty simple to rectify.
The first line will enable the rewrite module (which should already be available) and the next restarts the apache web server to reflect the new module addition.
If you have a nice and shiny new Zend Framework project all set up but are still getting nasty browser 404 errors when attempting to hit URL other than the home page (instead of the expected errors dealt out by the error handler controller), the root of your problem is actually quite simple:
Your Apache mod_rewrite module is disabled and your Apache directory AllowOverride directive is not switched on.
To fix is easy enough. Locate your Apache httpd.conf configuration file and open it up. Run a search for mod_rewrite and you should find the line:
#LoadModule rewrite_module modules/mod_rewrite.so
Uncomment it in order to enable, by removing the # character from the front of the line.
Next search for AllowOverride and you should find it in one of your declarations. If it is present, change the value to All. If it isn’t, add it. (Note if you are using virtual hosts you will need to set it under the virtualhost declaration). Setting this to all means that Apache will honour local .htaccess files, which is exactly what Zend, in conjunction with mod_rewrite, depends on.