Web Hosting Talk







View Full Version : mod_rewrite... can't get my head around it


Jeanco
05-15-2005, 12:49 PM
At one point I knew this, but I haven't used it in a while.

What code would I need for something simple, like rewriting

http://www.whdsfsdgffdg.com/index.php?content=whatever.php

to

http://www.whdsfsdgffdg.com/whatever

or

http://www.whdsfsdgffdg.com/whatever.html

Thanks :D

Oshaka
05-15-2005, 12:55 PM
Originally posted by Jeanco
At one point I knew this, but I haven't used it in a while.

What code would I need for something simple, like rewriting

http://www.whdsfsdgffdg.com/index.php?content=whatever.php

to

http://www.whdsfsdgffdg.com/whatever

or

http://www.whdsfsdgffdg.com/whatever.html

Thanks :D

First off you need a .htaccess in your case it should be


RewriteEngine on
RewriteBase /

RewriteRule A_NAME/([^.]+)/ index.php?content=$1
DirectoryIndex index.html index.php

errorDocument 404 /index.php


Making sure you change the A_NAME part ;)

Then save that file, reboot httpd for it to come into affect (linux only i think).

Should work with direct new urls then.

then your going to need this php snippet to get the new $_GET's out of the new urls


//get full url -> extract "GET"-data
$url = $_SERVER['DOCUMENT_ROOT'] . strtolower($_SERVER['PHP_SELF']);
$phpselfReg = "({$_SERVER['SCRIPT_FILENAME']})(\/)?(.+)?";
$getdata = eregi_replace($phpselfReg, "\\3", $url); if($getdata != "") {
$get = array();
$get = explode("/", $getdata);
if(count($get) > 0) {
} else {
}
} else {
}

// Use like so $something = $_GET[5] .. where 5 changes where the location is. to see the paths do a print_r($get);


Hope that helps!

- Josh.

Jeanco
05-15-2005, 01:19 PM
Thanks, a few questions though. What am I supposed to be changing the A_NAME to? I want the page to change with each page, so if $content=whatever.php then it will show as whatever.html and if its $content=somethingelse.php then it will show as somethingelse.html

Thanks Josh.

Oshaka
05-15-2005, 01:36 PM
You change A_NAME to what it will show up

ex: http://domain.com/A_NAME/whatever
ex: http://domain.com/A_NAME/somethingelse

or you could delete A_NAME and just make it

ex: http://domain.com/whatever (I believe, but i could be wrong.)


Hope that helps again.

- Josh.