30 December 2009

URL Rewriting



Introduction 

URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site. It can also be the source of near-unending misery and suffering. Definitely worth playing carefully with it - lots of testing is recommended. With great power comes great responsibility, and all that. There are several other guides on the web already, that may suit your needs better than this one.

  • Apache URL Rewriting Guide - The best guide around
Before reading on, you may find it helpful to have the mod_rewrite cheat sheet and/or the regular expressions cheat sheet handy. A basic grasp of the concept of regular expressions would also be very helpful.

What is "URL Rewriting"?
Most dynamic sites include variables in their URLs that tell the site what information to show the user. Typically, this gives URLs like the following, telling the relevant script on a site to load product number 7.
http://www.pets.com/show_a_product.php?product_id=7
 
The problems with this kind of URL structure are that the URL is not at all memorable. It's difficult to read out over the phone (you'd be surprised how many people pass URLs this way). Search engines and users alike get no useful information about the content of a page from that URL. You can't tell from that URL that that page allows you to buy a Norwegian Blue Parrot (lovely plumage). It's a fairly standard URL - the sort you'd get by default from most CMSes. Compare that to this URL:
http://www.pets.com/products/7/

Clearly a much cleaner and shorter URL. It's much easier to remember, and vastly easier to read out. That said, it doesn't exactly tell anyone what it refers to. But we can do more:
http://www.pets.com/parrots/norwegian-blue/

Now we're getting somewhere. You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines, whereas underscores are not), and they can use that information to better determine the content of the page. It's an easy URL to remember and to pass to another person.
Unfortunately, the last URL cannot be easily understood by a server without some work on our part. When a request is made for that URL, the server needs to work out how to process that URL so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
Platforms and Tools
Depending on the software your server is running, you may already have access to URL rewriting modules. If not, most hosts will enable or install the relevant modules for you if you ask them very nicely.
Apache is the easiest system to get URL rewriting running on. It usually comes with its own built-in URL rewriting module, mod_rewrite, enabled, and working with mod_rewrite is as simple as uploading correctly formatted and named text files.
IIS, Microsoft's server software, doesn't include URL rewriting capability as standard, but there are add-ons out there that can provide this functionality. ISAPI_Rewrite is the one I recommend working with, as I've so far found it to be the closest to mod_rewrite's functionality. Instructions for installing and configuring ISAPI_Rewrite can be found at the end of this article.
The code that follows is based on URL rewriting using mod_rewrite.
Basic URL Rewriting
To begin with, let's consider a simple example. We have a website, and we have a single PHP script that serves a single page. Its URL is:
http://www.pets.com/pet_care_info_07_07_2008.php
 
We want to clean up the URL, and our ideal URL would be:
http://www.pets.com/pet-care/
 
In order for this to work, we need to tell the server to internally redirect all requests for the URL "pet-care" to "pet_care_info_07_07_2008.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "pet_care_info_07_07_2008.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500"). If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine RewriteRule ^pet-care/?$ pet_care_info_01_02_2003.php [NC,L] # Handle requests for "pet-care"
A couple of quick items to note - everything following a hash symbol in an .htaccess file is ignored as a comment, and I'd recommend you use comments liberally; and the "RewriteEngine" line should only be used once per .htaccess file (please note that I've not included this line from here onwards in code example).
The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:
  • RewriteRule - Tells Apache that this like refers to a single RewriteRule.
  • ^/pet-care/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.
  • pet_care_info_01_02_2003.php - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.
  • [NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.
  • # Handle requests for "pet-care" - Comment explaining what the rule does (optional but recommended)
The rule above is a simple method for rewriting a single URL, and is the basis for almost all URL rewriting rules.


Source Link: URL Rewriting

36 comments:

Unknown said...

Very Nice,Important information for seo Candidate.

SHAMIM AHMAD said...

Really Nice Information.I get a Perfect Information Which was need for me

Baby Bedding said...

Very good information for seo.

gallery67 said...

Very important part of SEO. Thanks for giving tips.

SEO White Label Reseller said...

I have enjoyed reading your articles. It is well written. It looks like you spend a large amount of time and effort in writing the blog. I am appreciating your effort. .

Sonica said...

Great thoughts you got there, believe I may possibly try just some of it throughout my daily life




Search Engine Marketing Firm

t28 Tower Noida said...

Hi,I got a lot of information from this blog.

rize tower noida

the paras rize t28
rise tower noida
rise tower in noida
the paras rise t28

Seo training chennai said...

Thanks for sharing this article. You have shared lot of useful tips about SEO .It helped a lot to improve my online business.

SEO Chennai said...

Your article is really informative. Thanks for updating new information about marketing. Your information is really helpful for all online marketers. Thanks for sharing.

Seo training said...

Thanks for updating new information about seo. It helped me a lot to improve my businesses. Also share your new updated details about seo in your website. Thanks mate

Unknown said...

I really enjoyed the quality information you offer to your visitors for this blog. I will bookmark your blog and have my friends check up here often. SEO Company || SEO Services || SEO Company in Mumbai || SEO Services in Mumbai

Digipush said...

Nice post, digital marketing provide to improve your business, click the below links develope your business

Digital Marketing Agency
Seo Services India
Link Building Services India
Social Media Marketing Companies
PPC Company India

Bisla Bumrah said...

Great Post I really Like this, Keep sharing. Mobile App Development Company NYC
Website Development Company NYC
Website Design Company NYC

Dainiksatta said...

Great job i love this post keep blogging

Majestic Hair And Skin Clinic said...

Hi Thanks For Sharing this I really enjoyed reading this We are best Hosting Service Provider in india so if you are looking for cheap vps server please visit us

google pixel 3 said...

Wonderful post.This is nice post

Unknown said...

It is better to engaged ourselves in activities we like. I liked the post. Thanks for sharing.
python training in chennai | python training in bangalore

python online training | python training in pune

python training in chennai | python training in bangalore

python training in tambaram | python training in velachery

simbu said...

I have visited this blog first time and i got a lot of informative data from here which is quiet helpful for me indeed. 
java training in omr

java training in annanagar | java training in chennai

java training in marathahalli | java training in btm layout

java training in rajaji nagar | java training in jayanagar

Unknown said...

Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 


rpa training in Chennai

rpa training in pune

rpa online training

rpa training in bangalore

rpa training in Chennai

rpa training in Chennai

rpa training in velachery

rpa training in tambaram

luckys said...

activa 4g price

Unknown said...

Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.
Python training in marathahalli
Python training in pune
AWS Training in chennai

Anand said...

Good Article, Keep Sharing more like this!
Java Training in Chennai
Python Training in Chennai
IOT Training in Chennai
Selenium Training in Chennai
Data Science Training in Chennai
FSD Training in Chennai
MEAN Stack Training in Chennai

Anbarasan14 said...

This is an excellant blog. Thanks for taking time to share this information. Waiting for more updates.

IELTS Tambaram
IELTS Coaching in Chennai Tambaram
IELTS Classes near me
IELTS Velachery
IELTS Training in Chennai Velachery
IELTS Training in Velachery
IELTS Coaching Centre in Velachery

luckys said...

Dual Cup Photo Frame

sajid said...

Now we're getting somewhere. You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines.
tutuapp

Ahmad said...

Welcome,! So today in this article we're going to talk about one of the top Pay Per Download sites (PPD sites) and how we're going
to earn money with them.
updrives
https://updrives.com/blog
updrives

Unknown said...

freefireapk.info for android and pc

Unknown said...

Admiring the time and effort you put into your blog and detailed information you offer!..
showbox ios

admin said...

this is a very good and interesting site ..thanks for the informational content with us..
inverse trig identities

admin said...

Excellent effort to make this blog more wonderful and attractive.
pythagorean identity

rice purity said...

The material and aggregation is excellent and telltale as comfortably. ricepuritytestofficial.com/rice-purity-test-onlineRice purity test

official said...

Thanks for the informative and helpful post, showboxapk-official.com

Unknown said...

this is a very good site thanks for sharing this with us....mobdro older version

Anonymous said...

if you are looking For online Fun place especially Pakistani chatroom and People English Hindi German Chinese Speaking Urdu Sindhi Pashto Balochi Punjabi Kashmiri Siraeki and that is why GupshupFamily is best for Bringing everyone Together. So Follow the link www.GupshupFamily.com and Join ONLINE FREE PAKISTANI Chat Room Without Registration It's Totally Free Of Sign up u can easily sign up and chat there meet new people and make new Friends so what u waiting for hurry up and join our Pakistani Free Chat Room Without Registration. FREE ONLINE PAKISTANI CHAT ROOM WITHOUT REGISTRATION

Reshma said...


Awesome blog. Thanks for sharing such a worthy information....
Devops Strategy
Why Devops is Important

Hussey said...

Really nice blog. thanks for sharing
python training centre in chennai
best python institute in chennai