tags:

views:

67

answers:

3

What is the fastest way to beautify my urls?

I always have a single id ('page'). I want the url to be www.mysite.com/page

Thanks

+6  A: 

Assuming your website runs on Apache, you can use mod_rewrite.

Simply create a .htaccess file on your web server with something like this:

RewriteEngine On
RewriteBase   /
RewriteRule   (.*)  index.php?page=$1
casablanca
Given that the OP asks for the 'easiest way' to achieve his aim, you might want to explain how to use `mod_rewrite` to do this.
David Thomas
I just added that. :)
casablanca
Does that rule not rewrite infinitely? (I don't know much about mod_rewrite)
Michael Mrozek
Neither am I an expert, but according to [this thread](http://www.webmasterworld.com/forum10/1382.htm) it would only loop if the rule used a redirect.
casablanca
@casablanca =) (+1)
David Thomas
A: 

Several options:

  • Set a custom 404 ErrorHandler to direct all pages into /dispatcher.php. Parse the $_SERVER variables to know where to go.
  • Use MultiViews to automatically turn /foo.php/extra/path/info into /foo/extra/path/info.
  • Use mod_rewrite to do whatever complex thing you want.

mod_rewrite is the most complex to understand in its entirety, but it is (presumably) the most efficient method of the above and can do the most with the least amount of PHP code required.

konforce
A: 

I prefer using .htaccess to send all non-static requests to index.php and parse the $_SERVER['PATH_INFO'] variable and determine what code to run based on that, but in your case the easiest way may be to use mod_rewrite as already stated.

Hanse