Ad

.htaccess With Url Shortener

I've made a url shortener app , but can't seem to work my way around .htaccess to indeed make the urls shorter. (I'm using xampp)

my domain is http://localhost/smd i would like the user to be able to add a 7 digit code after smd http://localhost/smd/code and redirect them to their page my current .htaccess code is like that:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[a-zA-Z0-9_{7}]*$ redirect.php?shortened=$1  [L] 

which redirects the user to to a another page in my smd directory instead of the correct one. Thanks in advance!

my redirect file:

    <?php

include_once './class/Short.php';
date_default_timezone_set('Europe/Athens');

if(isset($_GET['shortened'])) {
    $short = new Short();
    $code = $_GET['shortened'];
    $short->shortened = $code;
    
    $short->getOneUrlByShort();
    
     $expire = $gt;getOneUrlByShort();
    
     $expire = $short->getExpiryFromShortened($code);
    
     $comp = (date('Y-m-d H:i:s')<=$expire['expiry_date']);
    $enabled = $short->is_enabled;

    if ($url = $short->getUrl($code) And $comp And $enabled == 1) {

        $redirect = $url['original'];
        header("Refresh: 3;".$redirect);
        die();
    }else if($url = $short->getUrl($code) And !$comp And $short->renewable == 1 And $enabled == 1){
        session_start();
        
        $short->renewable = 0;
        $newRenewVal = $short->renewable;
        
        $newExpiration = strtotime('+'.$short->active_period.' minutes');
        $formattedExpi = date('Y-m-d H:i:s', $newExpiration);
        
        $original = $url['original'];
        $_SESSION['original'] = $original;
        $_SESSION['expiration_date'] = $formattedExpi;
        

        $short->renewUrl($code, $formattedExpi, $newRenewVal);

       
        header('Location: http://localhost/smd/expiredAndRenew.php');
    }else {
        header('Location: http://localhost/smd/urlExpired.php');
    }

}



  ?>

My current directory looks like that 1

Ad

Answer

With your shown samples could you please try following. Please make sure you clear your browser cache before testing your URLs. Also this simply grabs everything coming after smd(with ignorecase emabled) in uri and passes it to your redirect.php as query string. In case you are looking for specific string/match for text after smd then kindly do let us know more clearly on that part.

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]{7})/?$ redirect.php?shortened=$1 [L]
Ad
source: stackoverflow.com
Ad