Variable Visions

Redirects compared - client vs server side

Published Wed. Oct. 31, 2012

Server side redirects and rewrites are the way to go but client-side redirects are ok if your redirecting a php header after a form is submitted or If you want the redirect to occur upon an event like a click to invoke a javascript redirect

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

javascript
<head>
<script type="text/javascript">?window.location = "http://www.domain.com/new.php";?</script>
</head>
OR
<button onclick="window.location = 'http://www.domain.com/new.php'">GO</button>
OR
<script language=”javascript”>

window.navigate(”http://www.domain.com/new.php”);

</script>
OR
<script language=”javascript”>

alert(”back”);

window.history.back(-1);

</script>
OR
<script language=”JavaScript”>

self.location=”http://www.domain.com/new.php”;

</script>

Of course these only work when a browser has Javascript turned on.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
html
There is an HTML way if your afraid js will be turned off. This client-side redirect is kinda old now though.

<meta http-equiv="refresh" content="0;url=http://www.domain.com/new.php"/>

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

php (server side)

<?php
header(" Location: http://www.domain.com/new.php");
?>

These seems common when redirecting a user after a form is submitted.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

apache (server side)


Redirect 301 /old.php http://www.variablevisions.com/new.php
OR
RewriteRule ^articles/(.*) /index.php?article=$1

http://www.variablevisions.com/articles/htaccess-and-MOD-REWRITE-Rules-Conditions-and-confusion

I use Redirect to send a user to a completely new and different url form the one they clicked on

I use Rewrite when I want to turn my ugly query string URLs into pretty SEO friendly URLs. This means that the same URL is then accessible form BOTH URLs. I just don't use the ugly urls in the site map or links. (hopefully to prevent duplicate content.

I also use a Rewrite condition and rule to redirect my url if typed without the www. I don't think I need the canonical tag here but what if someone links to my site without the three dubs? Will I get hit with a duplicate content slap?

http://www.variablevisions.com/articles/Canonical-or-301-Redirect


Next time, I'll talk about how to set up these redirects in the httpd.conf file rather then the .htaccess file to speed things up.



from the Apache site...
Rewrite vs Redirect
Apache has two ways to serve one page when you were asked for another:
Rewrite* rules via mod_rewrite (e.g. RewriteRule, RewriteCond), and
Redirect* rules via mod_alias (e.g. Redirect and RedirectMatch).
What are the differences?
    •    mod_rewrite is generally considered the more powerful, and more complex, solution. It offers the RewriteCond directive, which lets you only run a Rewrite rule if certain conditions are met.
    •    mod_alias is simpler, and lets you match on a simple string or on a regular expression, but does not allow you to put conditions in (other than those defined by the regexp)

Keywords:mod_rewrite, mod_alias, client-side redirect, server-side redirect