PDA

Click to See Complete Forum and Search --> : [RESOLVED] managing URLs


jimot
Oct 9th, 2006, 04:49 AM
hello.. how can I make a URL like this: http://www.mywebsite.com/?select=contact" to become "http://www.mywebsite.com/contact.html" ??

I see it all the time on the net. Thank you very much.

kows
Oct 9th, 2006, 05:31 AM
do you just want to redirect it, or do you want to take the input URL (?select=contact one), and transform it into the "contact.html" URL?

I'm going to assume you want to redirect, though, (although I'm not sure why, I would assume most people would ask how to make their page index.php?select=contact, instead of just contact.html.. let me know if that's what you meant!), so.. here's what you can try, and let me know if it works out for you:
<?php
//you can change "default" to be whatever you want the "main" file
//to be, so here i've made it index. if you don't enter a ?select,
//or the file isn't something you've set, then it will always go
//to the index
$select = (isset($_GET['select']) && $_GET['select'] != "") ? $_GET['select'] : "index";

switch($select){
case "contact":
//contact page
$page = "contact.html";
break;
//you can add other pages here as well, i've just added two to serve as an example
case "index":
//this is the -default- page,
$page = "index.html";
}

//now, we just redirect the user.
header("Location: http://www.mywebsite.com/" . $page);
?>
So, if you saved that as your index.php file, if anyone went to mywebsite.com/?select=contact, they would be redirected to mywebsite.com/contact.html instead.

edit: Also, if you wanted the index to still display normally, it's easy to change so that only certain pages get redirected and others display through the index. but, let me know if that was what you wanted.

Matt_T_hat
Oct 11th, 2006, 02:55 AM
hello.. how can I make a URL like this: http://www.mywebsite.com/?select=contact" to become "http://www.mywebsite.com/contact.html" ??

I see it all the time on the net. Thank you very much.The trick you are after uses MOD_REWRITE which is part of the Apache server.

For example I type lordmatt.co.uk/item/xxx but the script is called as lordmatt.co.uk/index.php?itemid=xxx

This is achieved using settings in .htaccess (a file) please note that ti starts with a dot and is therefore by windows standards all extension and no file name...

Options +FollowSymlinks
RewriteEngine on


This tells apache that it needs to be using rewrite. I'm unclear as to "Options +FollowSymlinks" but I understand it is best to use it.

There are a few things you can do

1. Set conditions
2. Rewrite
3. USe RegEx

This example corrects old urls for my wiki.


RewriteCond %{HTTP_HOST} !^wiki\.lordmatt\.co\.uk(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/wiki/ [NC]
RewriteRule ^wiki(.*)$ http://wiki.lordmatt.co.uk$1 [R=301,NC,L]

The first two lines look at the conditions the !means that URLS that are NOT wiki.lordmatt.co.uk and [NC] means No Case (case INsensitive).

The second condition means that the base url + /wiki/ is required in the URL

Then we take wiki and everything after it and put the everything to the end of the new url wiki.lordmatt.co.uk{{anything else}}.

[R=301] is HTTP code for permanant redirect which should be used only if you want the user to be aware of the change and tells the search engines to update thier records. [L] means last. If we are processing this line we process no more.

No most of the complex stuff is RegEx so if you are looking at it blankly (like I first did a year ago google a lot and eventually you will get the idea or be confused totaly :rolleyes: )

Here is what you might want to do using my domain as an example.

The example uses NucleusCMS which I run on lordmatt.co.uk


RewriteRule ^archive-([0-9]+)-([0-9]+)-([0-9]+).html+ index.php?archive=$2-$3&blogid=$1
RewriteRule ^item-([0-9]+).html+ index.php?itemid=$1
RewriteRule ^archivelist-([a-z]+).html+ index.php?archivelist=$1


I figure that should get you started.