Posts Tagged «htaccess»

Detour RedirectThere are many reasons for redirecting a user to another page. Perhaps the page has moved or perhaps they are trying to access a page they shouldn’t have access to. Or maybe you want to put in an interstitial ad (those full page ads displayed before taking you to the actual page you want to go to).

There are also many ways of implementing a redirect, each with advantages and disadvantages appropriate for different scenarios.

HTML Redirect/META refresh
This is probably one of the most widely used since it is easy to implement. Using a <meta> tag, once a page is loaded, users can be redirected to another page after a certain amount of time. All those “you will be redirected in 5 seconds” pages use a <meta> refresh tag to accomplish this.

The code goes in the <head></head> section of your HTML and looks like the following:

<meta type=”http-equiv” content=”7;url=http://www.jonlee.ca” />

The number before the semi-colon indicates how many seconds the browser should wait until redirecting the user to the page specified. If an URI is not specified, the page will refresh itself after the set amount of seconds — useful for continuously updating a page without having to use JavaScript.

The PHP Redirect (and other languages)
Using a server-side language, you can invisibly redirect the user to another page. For example, this can be useful if they try to access a secure page without proper authorization and you wish to redirect them to a login page.

The syntax varies with language but here is the PHP code:

<?php
header(”Location: http://www.jonlee.ca/”);
?>

This modifies the header to send the user to a desired page. Note that headers can only be modified if no output has been sent yet.

.htaccess Redirect
Using .htaccess to redirect has several purposes. Similar to forcing www, you can use mod_rewrite to send users to a different destination. The code is simple — redirect all page requests to a new domain.

RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

The advantage to this method is that it will maintain the page URI the user was trying to access. For example, if they navigate to http://www.olddomain.com/contact.php, they will be redirected to http://www.newdomain.com/contact.php instead of just the domain root.

Moved Permanently - 301 Redirect
Also, note the R=301 option at the end of the .htaccess code above. This 301 code is to let search engines know that the domain has been moved permanently and all old links should be updated to the new domain.

A 301 Redirect can also be achieved in PHP by adding this line prior to the redirection line:

header( “HTTP/1.1 301 Moved Permanently” );

If you’re into SEO (search engine optimization), listing the old domain as 301 is very important.

JavaScript?
There’s actually another method I left out and that is redirecting with JavaScript. I left it out because I can’t think of a situation where it would be better suited than the other methods described above. That, and there are more compatibility issues with JavaScript.

Know of any other ways of redirecting?

Popularity: 7% [?]

Tags: , , , , , ,

Comments 19 Comments »

If speeding up Google Analytics doesn’t give you enough of a performance boost in terms of page load time, then maybe this will be of interest to you. Page load time is directly proportional to the size of files and objects being retrieved. By compressing files before they are sent across to the end user, you can cut load times down by almost 40% (varies).

Since the file compression is done server side, there will be an increase of server load but it is usually very minor and the gain in bandwidth is well worth it.

The Tools
The module required for file compression is mod_deflate. If your web server is running Apache 2.x then it should already be installed. I know the word deflate sounds counter-intuitive but trust me, it compresses your files. (Similarly, you could also use mod_gzip, both produce similar results but mod_gzip isn’t pre-installed with Apache).

Getting Started
First step is to turn the module on in your .htaccess file. Simply put this line to turn on the mod_deflate module:

SetOutputFilter DEFLATE

That’s pretty much all you have to do, but while you have the .htaccess file open, you can add a couple more lines to maximize the performance boost. First, is to set the compression level to maximum:

DeflateCompressionLevel 9

Next, some files do not need to be compressed since they are already compressed. This includes zip archives, images and executables. The following line will bypass these file types for compression:

SetEnvIfNoCase Request_URI \
\. ( ?:gif|jpeg|jpg|png|exe|tgz|gz|zip|gz2|rar) $ no-gzip dont-vary

Biggest Performance Boosts
You’ll see the biggest boost in text files like JavaScript, CSS and HTML files. File sizes will be reduced by about 50%. Depending on the number of images you have on your site this translates to anywhere between a 15 to 40% decrease in page load time.

Incompatibilities
Netscape Navigator 2.0In order for this to work, the end user must be running a browser that supports compressed files. Pretty much all browsers these days do so this isn’t a big deal but to the handful of people still currently running IE4 or Netscape 6, the page will not display properly.

Popularity: 5% [?]

Tags: , , , , ,

Comments 8 Comments »

Everyone knows what a 404 File Not Found error is, we all get them from time to time. Now how do you capitalize on them? Simple, create a custom 404 error page (or any other error code) that fits in with your site’s template. A couple of benefits:

  1. It helps your users find what they were actually looking for.
  2. Increases your site’s exposure.

The easiest (and only) way I know of having custom error pages is to put the following into your .htaccess file:

ErrorDocument /link/to/yourpage.html

So to create a 404 error page, you would use:

ErrorDocument 404 /404error.html

Other popular error codes are:

  • 400 Bad Request - User has provided malformed syntax
  • 401 Unauthorized - Displayed when failing to access password protected pages
  • 403 Forbidden - User not allowed to view file (this pops up when you have directory indexing turned off and a user tries to access a directory’s file listing)
  • 404 File Not Found - self explanatory
  • 500 Internal Server Error - self explanatory

Popularity: 2% [?]

Tags: , ,

Comments No Comments »

One of the first things I do when I begin implementing a site is to force the www (or no-www) in the domain name. Why would I want to do this? For search engine optimization of course. For example, jonlee.ca redirects to www.jonlee.ca. What this means for Google is that it gets crawled as one site. Sites that link to jonlee.ca will add to the pagerank of www.jonlee.ca. Theoretically, this could double your Pagerank score (but not necessarily your Pagerank rating).

Anyway, here is the code to add to your .htaccess file to force www, but make sure mod_rewrite apache module is turned on:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.jonlee\.ca$ [NC]
RewriteRule ^(.*)$ http://www.jonlee.ca/$1 [R=301,L]

And here is the code to force no-www (in spirit of no-www.org):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.jonlee\.ca$ [NC]
RewriteRule ^(.*)$ http://jonlee.ca/$1 [R=301,L]

Of course, replace jonlee.ca above with your own domain name. To edit your .htaccess file you may need to enable view hidden files, or if the file doesn’t exist, you’ll have to create it yourself. The best way is to do it through an SSH connection to your server.

If you don’t have access to .htaccess, you could also accomplish the same thing through php with a simple script at the top of every page that will try to find www in the URL and redirect if found. Not the most elegant solution, but it does what it needs to do for those who can’t edit .htaccess for some reason.

if(!substr($_SERVER[’HTTP_HOST’], ‘www’)) //check if www exists in URL
{ header(’Location: http://www.jonlee.ca/’); //redirect to www.jonlee.ca
}

Or for those using wordpress, you could simply install a plugin that does all this for you! One example plugin is WWW-Redirect.

Popularity: 4% [?]

Tags: , ,

Comments 3 Comments »