Search code examples
php.htaccessurlvanity

Creating dynamic URL's with PHP


)

I am creating a dynamic event creation application and I have run into a problem when creating dynamic webpages for events.

My .htaccess looks like

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f  
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ table.php?event=$1 [L]

And my table.php looks like

$getEvent = explode("/",$_SERVER['REQUEST_URI']);
print_r($getEvent);
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}   
$result = $conn->query("SELECT * FROM event where link='$getEvent[4]'");
echo $getEvent[4];  

The page structure is as following :

http://page.ex/~name.name/reg/

And when I try to entry

http://page.ex/~name.name/reg/joulupidu

I get 404 although "joulupidu" is in event table. I have no idea where to look, because I haven't done much work with this kind of stuff before.

Thanks, WK!


Solution

  • Your .htaccess should be like

    RewriteEngine On
    RewriteRule ^([^/.]+)/reg/([^/.]+)?$ reg/table.php?event=$2&%{QUERY_STRING}
    

    And your table.php file shoul be like

    $getEvent = $_REQUEST['event'];
    //print_r($getEvent);
    $conn = new mysqli($host, $username, $password, $dbname);
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }   
    $result = $conn->query("SELECT * FROM event where link='$getEvent'");
    echo $getEvent;