Membuat alamat link URL menjadi cantik

This item was filled under [ Code Snippet, PHP ]

Rewrite URL adalah salah satu dari tenik SEO.
Berikut contoh simple pembuatan URL cantik menggunakan .htaccess

simpan file pada root folder project Anda dengan nama .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
 
RewriteRule ^(.*)$ index.php?do=$1 [QSA,L]

lalu pada file index.php Anda, mainkan aturan URL-nya.

contoh isi file index.php:

<?php
$do = explode ("/", $_REQUEST['do']);
$opsi = $do[0];
 
define('PUB_DIR', dirname (__FILE__) . '/' .);
 
switch($opsi) {
	case 'home':
		require_once (PUB_DIR . 'home.php');
	break;
	case 'content':
		require_once (PUB_DIR . 'web_content.php');
	break;
	case 'subcontent':
		require_once (PUB_DIR . 'web_content_sub.php');
	break;
	case 'news':
		require_once (PUB_DIR . 'news.php');
	break;
	case 'news-detail':
		require_once (PUB_DIR . 'news-detail.php');
	break;
	case 'articles':
		require_once (PUB_DIR . 'articles.php');
	break;
	case 'article-detail':
		require_once (PUB_DIR . 'article-detail.php');
	break;
	case 'event':
		require_once (PUB_DIR . 'event.php');
	break;
	case 'event-detail':
		require_once (PUB_DIR . 'event-detail.php');
	break;
	case 'calendar':
		require_once (PUB_DIR . 'calendar.php');
	break;
	case 'testimonial':
		require_once (PUB_DIR . 'testimonial.php');
	break;
	case 'testimonial-sign':
		require_once (PUB_DIR . 'guestbook_sign.php');
	break;
	case 'product':
		require_once (PUB_DIR . 'product.php');
	break;
	case 'product_search':
		require_once (PUB_DIR . 'product_search.php');
	break;
	case 'contact':
		require_once (PUB_DIR . 'contact_us.php');
	break;
 
	case 'search':
		require_once (PUB_DIR . 'search.php');
	break;
 
	case 'download':
		require_once (PUB_DIR . 'download.php');
	break;
 
	case 'faq':
		require_once (PUB_DIR . 'faq.php');
	break;
	case 'sitemap':
		require_once (PUB_DIR . 'sitemap.php');
	break;
 
	default:
		require_once (PUB_DIR . 'home.php');
}

Semoga membantu :mrgreen:

Rate this topic:
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5.00 out of 5)
Loading ... Loading ...
Popularity: 4,756 views
Tagged with: [ , , , , ]
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Artikel Sejenis

6 Comments on “Membuat alamat link URL menjadi cantik”

  • leo
    Mozilla Firefox Mozilla Firefox 3.0.15
    Windows Windows XP
    6 Nov 2009 9:41

    thanks atas tip htaccess nya mas/mbak. mau coba2 ah

    Like or Dislike: Thumb up 0 Thumb down 0
  • Suta
    Mozilla Firefox Mozilla Firefox 3.6
    Windows Windows Vista
    24 Feb 2010 12:04

    Misalkan kita tulis link url
    1) http://test/halo/ atau
    2) http://test/halo

    bagaimana cara membuat jika user mengetikkan seperti point 2 diatas agas selalu terdirect ke link pada point 1. Tks.

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 3.6
    Windows Windows XP
    6 Mar 2010 0:10

    @Suta,
    Coba pakai ini di file .htacess:

    RewriteCond %{HTTP_HOST} !^\.namadomain\.com$ [NC]
    RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
    Like or Dislike: Thumb up 0 Thumb down 0
  • Google Chrome Google Chrome 5.0.375.125
    Windows Windows XP
    8 Oct 2010 18:50

    Boss!! saya mo nanya, ini apabila saya ketik url
    point 1;
    http://alamatsitus.com/namasaya/ kemudian enter “muncul pesan dari apache server bahwasanya alamat tidak ditemukan – 404 not found – dan halaman situs pun hilang”.

    point 2;
    http://alamatsitus.com/namasaya/ kemudian enter “muncul pesan yang mungkin si admin web yang ngenulis – 404 not found – tanpa menghilangkan halaman situs.”

    apa pesan diatas menggunakan fungsi .htaccess? apabila iya, bagaimana sintak / penulisannya. atas jawabannya saya ucapkan terimakasih :D

    Like or Dislike: Thumb up 0 Thumb down 0
  • Mozilla Firefox Mozilla Firefox 3.6.8
    Windows Windows XP
    18 Oct 2010 19:49

    @smadav, halo,.. Anda bisa gunakan code htacess seperti ini:

    ErrorDocument 404 /halaman_error_saya.php

    dan dapat di custome sesuai keinginan Anda.

    Like or Dislike: Thumb up 0 Thumb down 0
  • Google Chrome Google Chrome 5.0.375.125
    Windows Windows XP
    20 Oct 2010 10:37

    Thank mas :D

    Like or Dislike: Thumb up 0 Thumb down 0

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Halaman ini di eksekusi dalam waktu 1.509 detik! (koneksi mayan bagus nih...)