Media Informasi

Media Informasi harian terkini

  • Home
Home » html , PHP , Website » How To Create .htaccess rewrite for Clean URLs

How To Create .htaccess rewrite for Clean URLs

  aditya    

.htaccess rewriterule examples - first example is smashing magazine

We are going to use .htaccess (hypertext access) rewrite rules to achieve these clean URLs. The .htaccess file provide a directory level configuration that is supported by several web servers such as Apache.
Our post for today covers the following contents:
1.0 Existing Apache .htaccess Rewriterule Examples
2.0 Four Apache .htaccess Rewriterule Example Codes
2.1 URL with two parameters: letter and number
2.2 URL with one parameter: name of the PHP file
2.3 URL with one parameter: numbers
2.4 URL with one parameter: numbers with underscore
3.0 Complete .htaccess Code Used
4.0 Apache .htaccess RewriteRule Resources

1.0 EXISTING OR LIVE APACHE .HTACCESS REWRITERULE EXAMPLES

Below are other example websites with clean URLs.
Of couse you know this blog, don't you?
Of couse you know this blog, don’t you? :))
Chris' blog
Chris’ blog
David’s blog
David’s blog

2.0 FOUR APACHE .HTACCESS REWRITERULE EXAMPLES

Our .htaccess and PHP files are placed in the “htaccess-clean-urls” folder.
.htaccess uses regular expressions to identify which PHP file will be loaded on the browser, based on the parameter(s) given.
We have 4 example codes below. Please note that “localhost” refers to “yoursite.com”. I was using my local server when I did these examples so please keep it in mind.

2.1 URL with two parameters: letter and number

This URL:
http://localhost/htaccess-clean-urls/parameter_letter_and_number.php?param=post¶m2=143
Is equivalent to this clean URL:
http://localhost/htaccess-clean-urls/post/143
.htaccess code used:
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ parameter_letter_and_number.php?param=$1&param2=$2 [NC]
PHP page used: parameter_letter_and_number.php
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Clean URL with .htaccess by http://codeofaninja.com/</title>
         
        </style>
    </head>
<body>
 
<h1>Parameter: Letter and Number ONLY. This is parameter_letter_and_number.php</h1>
<?php
echo "PARAMETER VALUE 1: " . $_REQUEST['param'];
echo "<br />";
echo "PARAMETER VALUE 2: " . $_REQUEST['param2'];
?>
 
</body>
</html>
Output screenshot:
.htaccess RewriteRule Example - parameter and number

2.2 URL with one parameter: name of the PHP file

This URL:
http://localhost/htaccess-clean-urls/login.php
http://localhost/htaccess-clean-urls/register.php
Is equivalent to this clean URL:
http://localhost/htaccess-clean-urls/login/
http://localhost/htaccess-clean-urls/register/
.htaccess code used:
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
PHP pages used:
login.php
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>login - Clean URL tutorial with .htaccess and PHP by http://codeofaninja.com/</title>
         
        </style>
    </head>
<body>
 
<h1>Login. This is login.php</h1>
 
</body>
</html>
register.php
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>register - Clean URL with .htaccess and PHP by http://codeofaninja.com/</title>
         
        </style>
    </head>
<body>
 
<h1>Register. This is register.php</h1>
 
</body>
</html>
Output Screenshots:
login page clean url
register page rewriterule example

2.3 URL with one parameter: numbers

This URL
http://localhost/htaccess-clean-urls/parameter_number.php?param=5254
Is equivalent to this clean URL:
http://localhost/htaccess-clean-urls/5254/
.htaccess code used:
RewriteRule ^([0-9]+)\/?$ parameter_number.php?param=$1 [NC]
PHP page used: parameter_number.php
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>parameter is number - Clean URL with .htaccess and PHP by http://codeofaninja.com/</title>
         
        </style>
    </head>
<body>
 
<h1>Parameter: Number ONLY. This is parameter_number.php</h1>
<?php echo "PARAMETER VALUE: " . $_REQUEST['param']; ?>
 
</body>
</html>
Output Screenshot:
htaccess rewriterule number example

2.4 URL with one parameter: numbers with underscore

This URL:
http://localhost/htaccess-clean-urls/parameter_number_and_underscore.php?param=143_5254
Is equivalent to this clean URL:
http://localhost/htaccess-clean-urls/143_5254/
.htaccess code used:
RewriteRule ^([0-9_]+)\/?$ parameter_number_and_underscore.php?param=$1 [NC]
PHP page used: parameter_number_and_underscore.php
<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>parameter is number and underscore- Clean URL with .htaccess and PHP by http://codeofaninja.com/</title>
         
        </style>
    </head>
<body>
 
<h1>Parameter: Number and Underscore ONLY. This is parameter_number_and_underscore.php</h1>
<?php echo "PARAMETER VALUE: " . $_REQUEST['param']; ?>
 
</body>
</html>
Output Screenshot:
htaccess rewriterule example - number and underscore

3.0 COMPLETE .HTACCESS CODE USED

The following codes are the ones we used in the examples above, put into one .htaccess file.
<IfModule mod_rewrite.c>
 
    RewriteEngine on
 
    RewriteRule ^([a-z]+)\/([0-9]+)\/?$ parameter_letter_and_number.php?param=$1&param2=$2 [NC]
     
    RewriteRule ^([a-z]+)\/?$ $1.php [NC]
     
    RewriteRule ^([0-9]+)\/?$ parameter_number.php?param=$1 [NC]
     
    RewriteRule ^([0-9_]+)\/?$ parameter_number_and_underscore.php?param=$1 [NC]
     
</IfModule>


Label: html, PHP, Website

Arsip Blog

Popular Posts

  • Contoh soal dan pembahasan Kalkulus 1 Integral
  • Membuat Login Form dengan PHP [Level Multi User]
  • Vocaloid 2 Hatsune Miku Full [Free Download]
  • Cara Import Database Ke Phpmyadmin yang File Nya *.opt *.frm *.MYD *.MYI
  • Cara Membuat Program Olah Data Sederhana menggunakan Netbean
  • Cara Menggunakan SSH di Android
  • Tutorial Mencari BUGS Host , Website , Server
  • Resident Evil 6 (2013) Full Version Pc Game Download Full Crack Patch iso
  • After "Submit", redirect back to previous page php
  • Buat text bergerak segala arah

Label

  • Trik (49)
  • software (49)
  • Info (47)
  • PHP (47)
  • tutorial (27)
  • Website (24)
  • Belajar (23)
  • Java (12)
  • Algoritma (11)
  • games (11)
  • Teknologi (9)
  • SEO (8)
  • berita (8)
  • film (8)
  • Design (6)
  • database (5)
  • promo (2)

Pengunjung

About

SEO Starter is SEO and Mobile Friendy Blogger Template. Responsive Sesuai dengan Rekomendasi Google

Web Links

  • Blogger Platform
  • CMS WordPress
  • Facebook
  • Microblogging
  • Manchester United

Follow by Email

Subsribe to get post update from this blog in your email inbox.

Copyright © Media Informasi. All rights reserved. Template by Romeltea Media