Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do URL re-writing in PHP?

I am trying to implement URL rewriting in my PHP application. Can someone share a step by step procedure of implementing URL rewriting in PHP and MySQL?

In my application I want to implement following URL rewriting, I want to redirect

1. http://example.com/videos/play/google-io-2009-wave-intro
2. http://example.com/videos/play/203/google-io-2009-wave-intro

to

1. http://example.com/videos/play.php?title=google-io-2009-wave-intro
2. http://example.com/videos/play.php?id=203

Please tell me how to implement both URL rewriting in any of the above way.

One more thing which URL will be best according to SEO, management, application point-of-view out of the following two types.

1. http://example.com/videos/play/google-io-2009-wave-intro
2. http://example.com/videos/play/203/google-io-2009-wave-intro
like image 754
djmzfKnm Avatar asked Jun 24 '09 17:06

djmzfKnm


People also ask

How rewrite URL in PHP?

Create a . htaccess file in your web servers test directory - or any other directory on which you want to make URL Rewriting active - and add the below given line to it. Now we have the rewrite engine turned on and Apache is ready to rewrite URLs for you.

What is URL rewriting explain with example?

URL manipulation, also called URL rewriting, is the process of altering (often automatically by means of a program written for that purpose) the parameters in a URL (Uniform Resource Locator). URL manipulation can be employed as a convenience by a Web server administrator, or for nefarious purposes by a hacker.


1 Answers

A Beginner's Guide to mod_rewrite.

Typically this will be nothing more than enabling the mod_rewrite module (you likely already have it enabled with your host), and then adding a .htaccess file into your web-directory. Once you've done that, you are only a few lines away from being done. The tutorial linked above will take care of you.

Just for fun, here's a Kohana .htaccess file for rewriting:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /rootDir/

# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/
RewriteRule .* index.php/$0 [PT,L]

What this will do is take all requests and channel them through the index.php file. So if you visited www.examplesite.com/subjects/php, you may actually be visiting www.examplesite.com/index.php?a=subjects&b=php.

If you find these URLs attractive, I would encourage you to go one step further and check out the MVC Framework (Model, View, Controller). It essentially allows you to treat your website like a group of functions:

www.mysite.com/jokes

public function jokes ($page = 1) {
  # Show Joke Page (Defaults to page 1)
}

Or, www.mysite.com/jokes/2

public function jokes ($page = 1) {
  # Show Page 2 of Jokes (Page 2 because of our different URL)
}

Notice how the first forward slash calls a function, and all that follow fill up the parameters of that function. It's really very nice, and make web-development much more fun!

like image 159
Sampson Avatar answered Sep 24 '22 21:09

Sampson