URL Rewrite PHP

Buat .htaccess

RewriteEngine on
 
RewriteRule ^/?halaman/([0-9]+)/(.*)$ r.php?h1=$1

Buat File r.php

<?php
$h1 = $_GET["h1"];

echo $h1;

Buat Index.php

Didalamnya ada function slug

<?php
function slugify($text)
{
    // replace non letter or digits by -
    $text = preg_replace('~[^\pL\d]+~u', '-', $text);

    // transliterate
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

    // remove unwanted characters
    $text = preg_replace('~[^-\w]+~', '', $text);

    // trim
    $text = trim($text, '-');

    // remove duplicate -
    $text = preg_replace('~-+~', '-', $text);

    // lowercase
    $text = strtolower($text);

    return $text;
}


?>


<a href="halaman/2/<?php echo slugify("ini adalah contoh halaman dari slug php") ?>">Tes</a>

Jalankan Index.php , maka akan muncul link tes, yang akan mengarah ke url

https://localhost/tes/halaman/2/ini-adalah-contoh-halaman-dari-slug-php

Maka Output

2

 1,936 total views

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *