Rewrite url?

Okay, I am looking on how to rewrite a url on my site, like:

http://www.site.com/index.php?id=8022

instead to be like:

http://www.site.com/8022

Any help?
Thanks!

Assuming you’re using Apache, you can create a .htaccess file in the root directory with:

<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L] </IfModule>

…this will redirect everything (that isn’t a real dir/file) to /index.php. Then in your index.php you can decide what to do with the request.

Does that help?

Sort of, what would be the exact code to do what I want in the original topic, I dont have any experience in the .htaccess but I want it so that I can rewrite (i guess its called) the url to not require it to have all the extra in there, just everything after “index.php?id=”. Any ideas on how to do this?

Thanks for the help though!

Ok… so, once the .htaccess is in place, the web server is going to send all requests to index.php… so if your link pointed to example.com/this/is/a/test, the contents in index.php will be displayed.

Your index.php needs to read the URI (this/is/a/test). You get all this - I’m just recapping :wink:

This function will take the URI and split it into an array based on "/"s.
[php]
function getRequest()
{
$uri = strip_tags(strtolower(trim($_SERVER[‘REQUEST_URI’])));
$uri = explode(’/’, $uri);
array_shift($uri);

if ($uri[0] == 'index.php') {
    array_shift($uri);
}

}

//Usage
$array = getRequest();
[/php]

Now that you have an array, it’s really up to you and your data schema as to whats done… a very rudimentary example might be:
[php]

$array = getReqeust();

if ($array[0] == ‘8022’) {
// maybe query a data base to get article id #8022
} else if ($array[0] == ‘myclass’) {
// maybe instantiate a class
$myClass = new myclass();
} else if ($array[0] == ‘filename’){
// maybe include a file file
// include ‘./filename.php’;
} else {
//echo a value
echo 'You requested: ’ . $array[0];
}
[/php]

This is all called ‘routing’ by the way… and in a nutshell (though not a very pretty example), thats how it works.

Wait… Im confused, wasn’t this suppose to be a .htaccess rewrite? I have heard of using it to rewrite the url…

I know how the ID of the page and run it as whatever I want, but it looks like your doing it in a complicated way, so Im looking for the .htaccess rewrite on how to do it.

Thanks for the help though!

I see what you’re asking, you want a full mod_rewrite - no php code involved solution. Your question wasn’t very clear on that part… or i’m just retarded, who knows :-X

Do something like this:

RewriteEngine On RewriteRule ^([^/]*)\$ /index.php?id=$1 [L]

Not tested…might need to play around with it but I think that’s right. Should rewrite any uri with index.php?id=### to /###/

That wasnt right, but it helped with what I was doing, I got it with a little tampering!

Thanks!

-Improvizionz

And after reading this and trying to apply to my site I don’t know what you tinkered to make it work?

Sponsor our Newsletter | Privacy Policy | Terms of Service