Mod_Rewrite Example, site.tld/USER

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Mod_Rewrite Example, site.tld/USER

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

    First is mod_rewrite in .htaccess file:

    Code:
    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:

    PHP Code:
    $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:

    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

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

    PHP Code:
    RewriteRule ^f([0-9]+)/.*?-([0-9]+)/index([0-9]+).html/$ topic.php?cid=$1&tid=$2&page=$[L]
    RewriteRule ^f([0-9]+)/.*?-([0-9]+)/index([0-9]+).htmltopic.php?cid=$1&tid=$2&page=$[L]
    //
    //
    // 
    then function to turn links to seo friendly.
    PHP Code:
    function SEO_Friendly($mysqli$SEO_Friendly$Data) {
    if(
    $SEO_Friendly == 'On') {
    if(
    preg_match('/topic.php\?cid=(\w+)&amp;tid=(\w+)&amp;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+)&amp;tid=(\w+)&amp;page=(\w+)/i''f\\1/'.$title.'-\\2/index\\3.html'$Data);
    return 
    $Data;
    }
    } else {
    return 
    $Data;
    }
    return 
    $Data;
    }

    //
    // 
    used like this:
    PHP Code:
    echo '<a href="'.SEO_Friendly($mysqli$SEO_Friendly'topic.php?cid='.$cid.'&amp;tid='.$tid.'&amp;page='.$page).'#'.$Row2['id'].'">#'.$Row2['id'].'</a>'."\r\n";
    //
    // 
    add this to config:
    PHP Code:
    $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&amp;tid=25&amp;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 '

    sigpic
    alt='coding-talk.com!!' />';
    echo 'Sharing Is Caring!';
    } else {
    echo '

    alt='the username GHOST has been comprimised!' />';
    echo 'OMG SOMEBODY HELP ME!!';
    }
    ?>

    Comment

    Working...
    X