Tag Archives: mod_rewrite

Apache: Simple way to Force a Domain Redirect Tips, Tricks and Tutorials 22 JUL 2014

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):

<IfModule mod_rewrite.c>
rewriteEngine on
rewriteCond %{HTTP_HOST} ^oldserver.com$
rewriteRule ^(.*) http://newserver.com$1 [L,R=301]
</IfModule>

Reload your apache configuration with

sudo service apache2 reload

and try to hit “oldserver.com/about” now.

Works well, doesn’t it? (And it wasn’t even difficult to do!)

apache-logo-banner

[UPDATE: Lately I’ve switch to placing this in the virtual host file:]

<VirtualHost *:80>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .? http://newserver.com%{REQUEST_URI} [L,R=301]
....

Quick way to prevent your Zend Controller from handling a Folder Tips, Tricks and Tutorials 30 JUN 2014

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.

zend-zf-logoQuick 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!)

WordPress: .htaccess Permalinks not Working (Ubuntu) Tips, Tricks and Tutorials 27 MAY 2014

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:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Next, give it appropriate file rights:

chmod 755 .htaccess

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.

horizontal wordpress logo landscape

Apache Webserver: A Method to Change the Domain of Your Site CodeUnit 28 JAN 2013

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:

<VirtualHost *:80>
ServerName myawesomeapp.com
RedirectMatch 301 ^(.*)$ http://myawesomeapp.com
</VirtualHost *:80>

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.

Clever.

Related Link: http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirectmatch

Ubuntu Server: How to Enable the mod_rewrite Module in Apache CodeUnit 13 APR 2011

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.

Using the a2enmod function, simply run:

sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

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.

Nifty.

Zend: How to fix the 404 Problem in a Zend Framework Project CodeUnit 23 MAR 2011

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.

Restart Apache, and hit a non home page URL.

And now you know.