Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Plugin: mod_rewrite

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
the-styler
Smarty Rookie


Joined: 12 Jul 2004
Posts: 25

PostPosted: Tue Feb 08, 2005 1:36 pm    Post subject: Plugin: mod_rewrite Reply with quote

Hallo,

ich möchte mal die chance nutzen und hier ein Plugin von mir vorzustellen. Die idee stammt nicht ganz alleine von mir. Sie wurde glaube ich hier im Forum schon einmal angesprochen. Aber ich denke ja, dass dies egal ist, da es darauf ankommt, dass der Code von mir stammt. Eigentlich ist das ganze System ziemlich simpel, aber es erfüllt den Zweck für den ich es brauche.

Ziel ist es, dass links nicht mehr in der Standart Form angeben werden müssen wie index.php?show=impressum und wenn man sie dann umschreiben möchte in mod_rewrite konforme links viel viel arbeit hat. nur das dann am ende da etwas rauskommt wie: index___show___impressum.html

Deswegen dachte ich mir, warum die Links nicht einfach in einer immer gleich bleibenden Form Smarty übergeben und die Umwandlung von Smarty vornehmen lassen. Also schrieb ich das folgende SCript:

Code:
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type:             function
* Name:             mod_rewrite
* Funktion:         erzeugt links passend für mod_rewrite
* Version:          1.0
* Date:             8 Februar 2005
* Last Modified:    8 Februar 2005
* Author:           Moritz Wörle
* HTTP:             http://www.simpsons-fansite.de
* -------------------------------------------------------------
*/

function smarty_function_mod_rewrite($params, &$smarty)
{
    if (empty($params['string'])) {
        $smarty->trigger_error("mod_rewrite: missing 'string' parameter");
        return;
    } else {
        $string = $params['string'];
    }

    if (empty($params['endung'])) {
        $smarty->trigger_error("mod_rewrite: missing 'endung' parameter");
        return;
    } else {
        $endung = $params['endung'];
    }

    $aufteilung = explode(",", $string);

    if($endung == "php") {
        $url = $aufteilung[0].".".$endung;
        unset($aufteilung[0]);

        $anz = count($aufteilung);

        if($anz != 0) {
            $url = $url."?";
        }

        for ($i=1; $i<=$anz; $i++) {
            if (($i % 2) != 0) {
                $url = $url."".$aufteilung[$i]."=";
            } else {
                $url = $url."".$aufteilung[$i]."&";
            }
        }

        if(substr($url, -1) == '&') {
            $url = substr($url, 0, strlen($url)-1);
        }
    } elseif($endung == "html") {
        $url = $aufteilung[0];
        unset($aufteilung[0]);

        $anz = count($aufteilung);

        for ($i=1; $i<=$anz; $i++) {
            $url = $url."___".$aufteilung[$i];
        }

        $url = $url.".".$endung;
    }

    return $url;
}
?>


dem script werden zwei werte übergeben. einmal die url in der entspechenden syntax und einmal die form wie mit dem link umgegangen werden soll.

Code:
{mod_rewrite string="index,show,impressum" endung="html"}


oder

Code:
{mod_rewrite string="index,show,impressum" endung="php"}


damit die html links auch verstanden werden hier noch die datei mod_rewrite die auch existieren muss

Code:
<?
/*
* Datei Name:    mod_rewrite.php
* Funktion:      übernimmt die umwandlung der angehängten variablen einer URL
*/

if(!$_SERVER['QUERY_STRING']) {
    die("kein direktaufruf erlaubt!");
}
unset($filename);
if(strpos($_SERVER['QUERY_STRING'],'&........................') === false) {
    $filename=str_replace('..','',$_SERVER['QUERY_STRING']);
} else {
    list($filename,$querystring)=explode('&........................',$_SERVER['QUERY_STRING']);

    $http_querystring=explode('___',$querystring);
    while(list(,$value)=each($http_querystring)) {
        if(!$value || !current($http_querystring)) {
            continue;
        }

        $varname=$value;
        $varvalue=current($http_querystring);
        $_GET[$varname] = $varvalue;
        $_REQUEST[$varname] = $varvalue;

        next($http_querystring);
    }
}

include './'.$filename.'.php';
?>


und der inhalt der .htaccess datei

Code:
RewriteEngine On

RewriteRule ^([^_]+)___(.+)\.html$ mod_rewrite.php?$1&........................$2 [L]
RewriteRule ^([^_]+)___\.html$ mod_rewrite.php?$1 [L]
RewriteRule ^([^_]+)\.html$ mod_rewrite.php?$1 [L]


Last edited by the-styler on Mon Jan 08, 2007 11:40 am; edited 1 time in total
Back to top
View user's profile Send private message
Silence
Smarty Rookie


Joined: 21 Jun 2004
Posts: 8

PostPosted: Sat Feb 19, 2005 3:50 pm    Post subject: Reply with quote

hi,

super idee - danke...

echt genial... ich hab auch schon seit einiger zeit immer mal wieder überlegt wie man das mod_rewrite probem dynamisch lösen kann...


gruss
silence
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP