wiki:faq/domain/redirect-web-users

Version 2 (modified by Ana Willem, 12 years ago) (diff)

--

How do I redirect web users from one page to another?

There are times when you are building a new website, where the organization of the new site varies so much from the old one that you can't use the same URLS as the old site did.

It's a good thing, when possible, to use old URLs because so many people bookmark certain pages. When users with old bookmarks go to your new website, you do not want to give them an unfriendly message saying simply that the page was not found.

There are a couple things that you can do to help make users who loved your site before continue to love your site now.

The first (ONLY FOR DRUPAL USERS) is to create friendlier pages for when: 1) people try going to a page that they thought was there and isn't and 2) people try going to a page that is outside of the realm of their permissions level.

Once you create those pages, you can navigate to the 'Site configuration' section of your administrative pages and click on the option that says 'Error reporting (mysite.com/admin/settings/error-reporting). In the spaces provided give the path or node number of the pages you created.

Examples of both kinds of pages: Access Denied: http://schr.org/naughty Page not found: http://schr.org/woops

The second thing that you can do is to create what is called a redirect. A redirect tells your site that when a person clicks on a specific old link, to go to a certain page on your new site. Information and ways to configure redirects are VAST and VARIED. There are two basic ones that this wiki covers the very basics of: Redirect, and RedirectMatch.

First, you will place the code for your redirect in the 'Web configuration' section of your 'Members Control Panel'. The code for a redirect goes after the end of the close of the directory tag (</Directory>).

The first thing you want to do is put the following text there:

RewriteEngine On

This tells your configuration that you are about to give it some code for what should happen when people enter specific URL that may or may not exist on your new site. Now for what comes after this...Technically, this text is not necessary specifically for the kinds of redirects that I am showing here. It doesn't hurt to have it especially in the case that you plan to use other Redirects that require it (RewriteCond or RewriteRule).

A simple Redirect tells your site that, for a specific page, to go to another specific page. The basic redirect looks like this:

Redirect [path] URL

In this example, '[path]' refers to the part of the URL that comes after the 'root' of your site. So for example the path of 'http://mysite.com/this/that/etc' is '/this/that/etc'. The path represents the address that you want to redirects users from. So if a user has bookmarked 'http://mysite/this/path' and you want them to go to 'http://mysite.com/another/path', the full redirect will look like this:

Redirect /this/path http://mysite.com/another/path

If you wanted to take them to a completely different site, you could do that also:

Redirect /this/path http://othersite.com/another/path

There are a lot of ways to do this and to use this function. If you want more, you can google it.

The other kind of Redirect involves a 'wildcard'. A wildcard is usually represented by an asterisk '*' and generally means 'anything'. It's like in poker when I play a wildcard, I can make that card be anything I want.

The reason that it becomes useful is that the standard website has the following structure:

http://mysite.com/firstpart
http://mysite.com/firstpart/secondpart
http://mysite.com/firstpart/secondpart/thirdpart 
http://mysite.com/firstpart/secondpart/thirdpart/etc

If you wanted to redirect everypage that had a path that started with'/firstpart/' then you will need a wildcard to do that. It prevents you having to create a redirect for every single webpage on your old site.

The simplest RedirectMatch is written like this:

RedirectMatch 301 ^/firstpart/(.*)$ http://mysite.com/other_firstpart

So in this case, it is telling the configuration that when a user wants to go to the page:

http://mysite.com/firstpart, or 
http://mysite.com/firstpart/secondpart, or 
http://mysite.com/firstpart/secondpart/thirdpart, or 
http://mysite.com/firstpart/secondpart/thirdpart/etc...

to send those users to:

http://mysite.com/other_firstpart.

This is, again, a very condensed tutorial that is designed to just tell you one way to do things, and not in a way that teaches you deeper principles of these functions. There are other ways to use them as well. Some sites that go into this in more detail include:

http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch or, http://www.baldwinit.com/redirectmatch-with-wildcards.html or, http://www.askapache.com/htaccess/301-redirect-with-mod_rewrite-or-redirectmatch.html

The option that we did not discuss here is to use an .htaccess file to redirect folks. This isn't recommended and the reasons why is in this ticket:

https://support.mayfirst.org/ticket/720