url rewriting using .htaccess

June 12th, 2012 by Laeeq | 5 comments

Htaccess Rewrites are enabled by using the Apache module mod_rewrite, it is one of the most powerful Apache modules and features availale. Htaccess Rewrites through mod_rewrite which provide the special ability to Rewrite requests internally as well as Redirect request externally.

If you are looking for URL rewriting example then this post might be useful for you.

Here I have given few useful examples of URL rewriting using .htacess.

Now let’s look at the examples

  1. Rewriting item.php?id=67 to item-67.html

Above is a simple redirection in which .php extension is hidden from the browser’s address bar.

  1. RewriteEngine on
  2. RewriteRule ^item-([0-9]+)\.html$ item.php?id=$1
  3. Rewriting item.php?id=67 to item/iphone5/67.html

In the above URL rewriting technique you can display the name of the item in URL.

Redirecting non www URL to www URL

You have seen when we type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection.

  1. RewriteEngine On
  2. RewriteCond %{HTTP_HOST} ^phpzag\.com$
  3. RewriteRule (.*) http://www.phpzag.com/$1 [R=301,L]

Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
  1. RewriteEngine On
  2. RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1\
  3. RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

Redirecting the domain to a new sub folder inside public_html

Below is the .htaccess file code to redirecting the domain to a new subfolder inside public_html

  1. RewriteEngine On
  2. RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
  3. RewriteCond %{HTTP_HOST} ^www\.test\.com$
  4. RewriteCond %{REQUEST_URI} !^/new/
  5. RewriteRule (.*) /new/$1

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Wrapping Text using PHP
  • PHP 5.5 beta 4 is now available
  • Swapping Array Keys and Values
  • PHP Security
  • Working with Image Metadata in PHP
  • Converting Between Relative and Absolute File Path
  • Parsing File Paths with PHP
  • How to remove HTML Comments with PHP?
  • How to access .htaccess values?
  • Dealing With Common PHP Errors
  • Interview Questions and Answers for Freshers
  • Display numbers with ordinal suffix using PHP
  • Function to set COLLATION on database fields of Mysql
  • Read and write to remote files using PHP
  • Sharing PHP SESSION Variables between Multiple Subdomains
  • PHP 5.5.0 Alpha4 Development Preview Released
  • Check a string starts with http using PHP
  • Cross-Site Scripting Attacks (XSS)
  • PHP 5.4.11 and PHP 5.3.21 released!
  • PHP Memory Leak Issue
  •  

     

    1. Rajasekar
      July 8th, 2012 at 11:58 | #1

      Thanks for sharing Quick url rewriting using .htaccess tutorials

      It works like a charm

    2. jaydenpiercydz
      October 3rd, 2012 at 12:48 | #2

      You can certainly see your expertise within the paintings you write. The sector hopes for even more passionate writers such as you who are not afraid to say how they believe. Always go after your heart. “Experience is a good school, but the fees are high.” by Heinrich Heine.

    3. vida
      February 12th, 2013 at 15:40 | #3

      Good day very nice site!! Guy .. Excellent ..
      Superb .. I’ll bookmark your site and take the feeds also? I am satisfied to search out a lot of helpful information right here in the publish, we want develop extra strategies on this regard, thanks for sharing. . . . . .

    4. February 16th, 2013 at 07:22 | #4

      What’s up, yup this paragraph is actually pleasant and I have learned lot of things from it concerning blogging. thanks.

    5. lengthy eyelashes
      February 22nd, 2013 at 23:39 | #5

      Wonderful article! This is the type of information that are meant to be shared across the web.
      Disgrace on the seek engines for now not positioning this publish higher!
      Come on over and consult with my web site . Thanks =)

    1. No trackbacks yet.