Gryphyn Media web hosting - Philapdelphia PA
Hosting for people who know a thing or two.

Plans | Features | Network | Policies | Support | About | Contact

Jump to HelpDesk | Announcements | FAQ | Webmail Login


Match word(s).

How can I use mod_rewrite to make my URLs more search engine friendly?

This FAQ explain how to change an example URL
http://www.yourdomain.com/news.php?id=24
to
http//www.yourdomain.com/news/24
to encourage search engines to index the page.

If you do NOT KNOW HOW to find and make files in your directory structure, this method is too advanced for you.

In your .htaccess file of your document root (i.e. /public_html), put the following two lines:

RewriteEngine on
RewriteRule ^news/([0-9]+)$ /news.php?id=$1

.htaccess doesn't exist by default, so you may have to create it. It's just a normal text file. Some ftp clients won't list it unless explicitly told to do so. It WILL appear in the File Manager of your control panel.

The directives above mean, in essence, "turn on the rewrite engine, and here's a rule to check for."

The RewriteRule is a regular expression that the mod_rewrite engine tries to match each and every page request to. It is always in the form of:

RewriteRule pattern-to-find substitution-to-rewrite-to [OPTIONAL FLAGS]

Each portion of the directive is separated by 1 space.

Looking at this regular expression in particular...

The first part of the directive "^news/([0-9]+)$" is the regex pattern the engine will try to match.

In this context, the "^" character means to "anchor" the regex and start the match at the beginning of the requested url.

The "news/" characters are just regular characters, and should match exactly to the requested url.

"[0-9]" means to match only digits.

The "+" means to match the preceding expression (only digits) at least 1 or more times.

The surrounding parentheses mean that the regex engine should "capture" any matches it finds within and save them for later use.

The "$" character means to "anchor" the regex to the end of the requested url.

So in plain English this might read: "Starting at the beginning of the page request string, match "news", then a forward slash, then any digits, until the end of the page request string. and save the digits for later reference."

The second part, "/news.php?id=$1", is the substitution url to rewrite the request to if the engine has matched the preceding pattern.

It is pretty self-explanatory, except for the "$1". this is a backreference to the parentheses in the pattern. think of it as a placeholder for any "captured" matches found in the preceding pattern (remember the parentheses?). Each backreference will be replaced by the corresponding parentheses in the pattern. So if you have 2 parenthetical expressions in the pattern, you can have backreferences $1 and $2 in the substitution. You can have up to 9 backreferences.

In plain English this might read: "Rewrite (and reroute) the page request you just found a match for, to news.php?id="

A Google search on "mod_rewrite" will help you find more tutorials about this method.

Related Topics:
How can transfer large files to other people without using email?
What perl modules are installed?
What do you have for multiple domains?
How do I get Dreamweaver to connect to my account?
What do the different terms mean in the statistic programs?
How can I help make the shared server more secure?
How do I use the shared SSL certificate?
Can I set up ODBC access to a database on the server?
Why does my form redirect to a "contact support" message?
How can I see/edit my .htaccess file?
Do you run SUexec?
What are the options for sending an email list or newsletter?
Is there telnet?
Can I get shell access?
Do you support Cold Fusion?
Do you do Windows hosting?
How can I manage downloads from my site?
Do I need PHP? Should I learn it?
Can I use Frontpage 2003?
How do I install my own SSL certificate?
I need to install a form on a website... what should I use?
What port do I use to access shell via SSH2?
Are your servers mirrored, or otherwise "bullet-proof?"
I want to sell things from my site. What do I need?
Can you recommend an SSH client?
How can I keep certain pages from being indexed by search engines and other spiders?
How can I install WordPress and get started using it?
Can I use PHP and SSI together?
Where can I find a good tell-a-friend referral script?
How can I more easily move accounts from another host?
How can I block certain IP addresses?
My form is "breaking" my page layout.
How do I use the Auto-Installer?
How do I set up a database?
I need help with SSI (Server Side Includes).

Developer Note: This is powered by SmartFAQ, a free cgi script.


©Gryphyn Media, Inc 1998-2003
about | policies | contact | philadelphia