Virginia Beach Web Development | Website Developer | Mobile Dev | Mobile Video Integration | Doodersrage

Just about everything Website Development related. Virginia Beach VA headquartered with a global reach!

Redirecting documents with PHP, Apache, and MOD_REWRITE

Posted on | January 21, 2012 | No Comments

feather small Redirecting documents with PHP, Apache, and MOD REWRITE

In document PHP redirect with HTTP code assignment:

header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://www.domain.com/new-location/" );

The two lines above will redirect the document request to the /new-location/ page if nothing has been printed to the screen. If this method if attempted after printing something to the screen or flushing the output buffer you will instead receive an error. This method is great to use if you do not have access to the HTACCESS document.

By default the –header(“Location:”)- redirect method applies a 302 status code.

This same method can be used to redirect and apply a 404 status code.

header("HTTP/1.1 404 Not Found");
header( "Location: http://www.domain.com/error/" );

Alternately you can set the 404 status code but remain in the same document.

header("HTTP/1.1 404 Not Found");

If the above line was placed within a 404.php document it would then assign the correct status code of 404 to the document header.

Changing -404 Not Found- to whatever status code you would like to use will then assign that status code.

Rewrites using the HTACCESS file:

Within any Apache environment or just about any there is a hidden file names -.htaccess- (the dot signifies that the file is hidden) this file allows for many actions to be applied to your web application but we are just going to focus on page request redirection.

Redirecting without mod_rewrite:

Redirect 301 /oldpage.html http://www.domain.com/newpage.html

The above line applies the 301 header when –oldpage.html- is requested then redirects the request to the –newpage.html- document. You can replace the 301 with a 302 for a temporarily moved page.

Assigning a 404 or any other kind of error page:

ErrorDocument 404 /404.php

This line tells apache that the requested document is an error document that should have the status code of 404 applied to it. A relative link is used to load the document within the current URI request. You can assign a direct path to the document but that would also invoke a 302 redirect after the 404. This method should not be used due to the double header assignment.  For other error status codes just replace the -404- after the –ErrorDocument- call.

Redirecting documents with MOD_REWRITE:

Using MOD_REWRITE can get confusing quickly! Luckily a good cheat sheet exists! http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/

When using MOD_REWRITE you will have to make sure that your rewrite listing follows after the two lines below.

Options +FollowSymLinks
RewriteEngine on

The first line sets the option to allow the following of symbolic links while the second line turns the document rewriting system on.

RewriteRule ^oldpage.php  http://www.domain.com/newpage.php [R=301,L]

The line above looks for any document request starting with –oldpage.php- then assigns the 301 status code and redirects the user to the direct linked –newpage.php- document. The –R=301- assigns the page status code while the –L- tells the MOD_REDIRECT to stop processing the assigned redirects. The –L- assignment is best practices as it prevents any future redirects and makes sure the users page load remains speedy. Redirects set within the HTACCESS file are read line by line of each page load. Your best bet is to keep this list as short as possible.

Redirecting all requests to another domain:

RewriteRule ^/(.*)  http://www.domain.com/$1 [R=301,L]

This redirect looks very much like the previous one but uses a wild-card variable assignment. This is then thrown to the -$1- variable.

Redirecting non-www to the www sub-domain:

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

The above sets the rewrite condition of the domain without an assigned www sub-domain then redirects the user and the requested URI to the www sub-domain.

Well that’s the basics!

Comments

Leave a Reply