Jump to content
xisto Community
Sign in to follow this  
Yacoby

Using The Rewriterule .htaccess

Recommended Posts

Quick into on .htaccess

 

.htaccess (note the dot) is a text file, each command is meant to be placed on a single line, so if your text editor has word-wrap, make sure it is turned off.

 

When you upload it to your server, make sure it is uploaded in ASCII mode rather than binary.

 

What is mod_rewrite?

 

mod_rewrite is a apache modal that allows you to rewrite URLs at a server level. For example, when a user requests the page

 

http://forums.xisto.com/no_longer_exists/

 

mod_rewrite could rewrite the URL so that the sever really gives this page

http://www.domain.com/index?page=disks

 

 

Why should I use mod_rewrite?

 

The problem is that search engines used to not search urls when they had characters like ? and = in. These characters usually occurred when you had a dynamic website, with URLs like http://www.domain.com/index?page=disks∏=246, it created a nightmare in SEO (Search Engine Optimization). What they used do was chop off the URL after the none standard character, so if you had a set of URLs:

 

http://www.domain.com/index?page=disks

http://www.domain.com/index?page=paper

http://www.domain.com/index?page=somepage

 

Every URL would end up as

 

http://www.domain.com/index?

 

 

Thankfully, most search engines can crawl dynamic URLs, unfortunately, they don't do it as efficiently as they do it with static URLs and in some cases they don't crawl every page. It also has benefits like allowing you to change the language the site runs on. For example, it you converted your site from asp to PHP, no urls would need to change, so you wouldn't have any problems with 404 errors

 

How do I use mod_rewrite?

 

The first thing you need to do is check you have mod_rewrite installed on your server. Create a php page that looks like so:

 

<?phpphpinfo(); ?>
Upload it to your server. Load the page in your web browser and perform a search for mod_rewrite, you should find it under the "Loaded Modules" section. If you can't find it, you will have to contact your hosting company and ask them to add it.

 

Note: Xisto has this modal, so you don't need to do this if you are hosted at Xisto.

 

Basic Example

 

The most basic form of mod_rewrite is like this.

 

RewriteEngine onRewriteRule ^apage.php$ anotherpage.php
This would redirect someone who loaded apage.php to anotherpage.php, without changing the URL that they see in their browser address bar

 

RewriteEngine on

Turns the Rewrite engine on, without this we could do nothing

 

RewriteRule

This is the name of the command

 

^apage.php$

The ^ and the $ are the beginning and the end of the anchors respectively. They are called anchors because they "anchor" the RewriteRule in a specific position in the URL string.

 

anotherpage.php

This is the page that the URL is re-directed to.

 

 

More Complex Examples

 

Example One

 

RewriteEngine onRewriteRule  ^cms/([^/]+)/?$ index.php?page=$1 [L]
This will redirect a URL like:

http://forums.xisto.com/no_longer_exists/

to

http://www.domain.com/index?page=disks

 

^cms/

If the URL requested page begins with cms/

 

([^/]+)

Anything within brackets is assigned to a variable like $1 or $2 etc. This gets one or more characters that isn't a forward slash, and assign it to a variable.

 

 

/?$

This makes sure that the requested URL ends with a / or nothing else. In more detail, the / is the character that it checks if it ends with, the ? makes the character

optional, so it can be a forward slash or nothing, and the $ is the end of the anchor.

 

 

index.php?page=$1

This redirects the page to index.php?page=$1. The $1 is the text that is in ([^\]+) . If you had more than one set of brackets, like ^cms/([^\])/([^\]+)/?$, the second

set of brackets would have the backreference $2

 

[L]

This makes it so that if the RewriteRule is successful, it is the last rewrite rule to be executed.

 

 

Example Two

 

RewriteEngine onRewriteRule ^download/([^/]*\.)(zip|exe|bat)$ files/$1$2
This will redirect a request for

http://forums.xisto.com/no_longer_exists/

to

http://forums.xisto.com/no_longer_exists/

 

RewriteRule ^download/

The URL must start with download/

 

([^/]*\.)

This matches and assigns to a variable anything that isn't a forward slash, and ends with a ".", the "\" before the period is required to signify that the period is a character rather than a command. (The period on its own matches any character, not what we want!). Notice we have uses an asterisk (*) instead of a plus (+), this means get zero or more characters rather than one or more characters

 

(zip|exe|bat)$

This matches a zip or exe or bat, and assign it to a variable

 

files/$1$2

This redirects the request to real file using the variables assigned.

 

 

Example Three

 

If you have a dynamic URL that contains numeric sections, like so:

http://www.domain.com/index??id=231&page=1

 

If you don't want any non numeric character, you can use this

 

RewriteEngine onRewriteRule ^articles/([0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2
This would re-direct a page request like:

http://forums.xisto.com/no_longer_exists/

to

http://www.domain.com/index??id=231&page=1

 

The only new part in this is this:

([0-9]+)

This gets one or more numbers between 0 and 9, you can also specify one or more lower case characters using ([a-z]+), if you wanted uppercase, you would change it to ([A-Z]+).

 

Tutorial Copyright Jacob Essex 2006

This may not be copied or mirrored without permission

 

-------------------------------------------------

 

Any comments on this would be appreciated, as this is the second tutorial I have ever written, so I would welcome constructive criticism.

Edited by Yacoby (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.