Mod_Rewrite Example, site.tld/USER

Started on the old forum in April 2013 · view the original in the archive · old links redirect here

1 reply · 617 views · started 13 years ago · 1 min read

2 participants
arnage Lead · 811 posts #1

This simply changes the dynamic parts of url, such is page.php?var=$user.

First is mod_rewrite in .htaccess file:

RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteRule ^([0-9Aa-zZ]+)/$ page.php?var=$1 [L]

This means that from the part $1 will be token if the content is lower-upper case letters and numbers and set it after RewriteBase which is domain.

Next is setting or changing the links through the script such as ./page/var instead ./page.php?var=var

But if you have done script and just want to change this way, for example, for users, it is much easier to write a redirect page to avoid editing the whole script. That is pretty much like this. Make redirect.php with:

$url = !empty($_GET['query']) ? (preg_match('/^[a-z0-9]+$/iD', $_GET['query']) ? $_GET['query'] : null) : null;
if (isset($url)) {

	$url = trim($url);
	if (get_magic_quotes_gpc()) $url = stripslashes($url);
	$query = mysql_query('SELECT * FROM `users` WHERE `nick` = "'.mysql_real_escape_string($url).'" LIMIT 1');
	if (mysql_num_rows($query)) {

		$id = mysql_fetch_array($query);
		$go = (int) $id['id'];

		header('Location: ./index.php?id='.$go.'');
		exit;

	} else {
		header('Location: /');
		exit;
	}
} else { exit('Redirect Error'); }

With .htaccess code:

RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteRule ^([0-9Aa-zZ]+)/$ redirect.php?query=$1 [L]

Thats it. And if you have some other GET vars across this rewrite, you would have to add it in both parts.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Ghost Senior · 366 posts #2

here is my way of making topic links identical to coding-talk topic links.

RewriteRule ^f([0-9]+)/.*?-([0-9]+)/index([0-9]+).html/$ topic.php?cid=$1&tid=$2&page=$3 [L]
RewriteRule ^f([0-9]+)/.*?-([0-9]+)/index([0-9]+).html$ topic.php?cid=$1&tid=$2&page=$3 [L]
//
//
//

then function to turn links to seo friendly.

function SEO_Friendly($mysqli, $SEO_Friendly, $Data) {
if($SEO_Friendly == 'On') {
if(preg_match('/topic.php\?cid=(\w+)&tid=(\w+)&page=(\w+)/i', $Data, $matches)) {
$query = $mysqli->query('SELECT * FROM `topics` WHERE id="'.$matches[2].'" LIMIT 1') or die($mysqli->error);
if($query->num_rows > 0) {
while($Row = $query->fetch_assoc()) { $title = str_replace(' ', '-', $Row['topic_title']); }
}
$query->free();
$Data = preg_replace('/topic.php\?cid=(\w+)&tid=(\w+)&page=(\w+)/i', 'f\\1/'.$title.'-\\2/index\\3.html', $Data);
return $Data;
}
} else {
return $Data;
}
return $Data;
}

//
//

used like this:

echo '<a href="'.SEO_Friendly($mysqli, $SEO_Friendly, 'topic.php?cid='.$cid.'&tid='.$tid.'&page='.$page).'#'.$Row2['id'].'">#'.$Row2['id'].'</a>'."\r\n";
//
//

add this to config:

$SEO_Friendly = 'On';
//
//
//

results if seo option is ON: site.tld/f2/title-here-25/index1.html#92

result if seo option is OFF: site.tld/topic.php?cid=2&tid=25&page=1#92

seo link description: f2 = category/forum (2) title-here-25 = topic name AND topic id (25) index1.html = page (1) will change to index2, index3 etc #92 = topic post id (92) for jumping to post

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close