I have been experimenting with .htaccess a lot regarding with mod rewriting and URL rewriting and had a lot of difficulty finding the solutions regarding to exact problem I faced. This stuff is such a headache. I am feeling headache right now as I am typing after messing with a lot of .htaccess codes. Here is an update regarding essential stuff which you will find useful if you are trying to change your URLS from index.php to index.html or index.php to index and if after putting RewriteEngine on images are not showing up at pages. Following exercises will answer these tasks in htacces mod rewrite:

1- How to enable RewriteEngine for htaccess and localhost
2- Images are not showing after RewriteEngine on
3- Images in second level directory are not showing after RewriteEngine on
e.g. images/1.jpg is showing but images/icons/2.jpg is not showing up
4- Want to change index.php into index or about.php into about
5- Want to change all pages.php to pages.html for site using htaccess
6- Want to Rewrite yoursite.com/user.php?username=xyz to yoursite.com/xyz
7- Want to Rewriting product.php?id=12 to product/ipod-nano/12.html
8- Want to change product.php?id=5 to porduct-5.html

Enable ReWrite Engine

Before rewriting do this in .htaccess file.

Options +FollowSymLinks
RewriteEngine on

and after that start rewriting

Images not showing after RewriteEngine on

Solve images problem which sometimes does not show if you enable rewrite engine in .htaccess. I was trying to change index.php to index and when I did that, images from first images directory were displaying (images/) but the second level images (images/icons/) were not displaying. Even when I tried the absolute path, images from images/icons/ directory were not displaying. The trick is that, you have to rewrite images directory as well and similarly sometimes css and js directories also. In this case when I tried the following code, my problem was solved.

RewriteRule ^images/(.+)?$ images/$1 [NC,L] 
# RewriteRule ^images/icons/(.+)?$ images/icons/$1 [NC,L]

First line in above example solved my problem and I did not need the second in my case.

There is problem with Browsers which don’t recognize that folders you typed are not real So there is problem with images and css style. In another case consider the following example.

All you have to do is mod rewrite all your image, css and js folders like below:

RewriteRule ^pictures/images/(.+)?$ images/$1 [NC,L]
RewriteRule ^pictures/css/(.+)?$ css/$1 [NC,L]
RewriteRule ^pictures/js/(.+)?$ js/$1 [NC,L]
RewriteRule ^pictures/php/(.+)?$ php/$1 [NC,L]
RewriteRule ^pictures/([0-9]+)?$ pictures.Php?year=$1 [NC,L]

Redirect index.php to index

Okay, you have to do this with all the pages separately. In the following case I have done these with two pages. Index.php and about.php. I don’t know yet what is the solution for doing it in single line for every page.

RewriteRule index index.php
RewriteRule about about.php

so on…

Important: You have implemented this code in .htaccess file. Now your like for yoursite.com/about.php will be yoursite.com/about. You will have to link the text with this modified call (with no .php) and .htaccess will do the magic where you have defined the rule to change about.php to about. That’s it.

Change .php URLs in .html

Now this one I know. Single line of code will do the magic. You have all the pages of site in php, but after writing this single line code in .htaccess, they will be changed into .html. I mean the URLs will be changed to .html. Link your pages with .html extension and write this code in .htaccess file and you are done.

RewriteRule ^(.*)\.html$ $1.php [NC]

For htm do this.

RewriteRule ^(.*)\.htm$ $1.php [NC]

Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

Rewriting product.php?id=12 to product/ipod-nano/12.html

RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

And change product.php?id=5 to porduct-5.html so that http://localhost/product-5.html calls product.php?id=5 automatically

RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1

Done…

Please help me learn and do more with this htaccess stuff. If you know more about this, want to correct / improve these codes or offer more advanced codes to do the same what I have mentioned above, share with me in comments.

  • Share/Bookmark

2 Reviews

  1. phil says:

    nice article..
    but, how can i do if i don’t want to display “id” in my URL, something like this
    http://www.example.com/product.....itle-here/
    instead of
    http://www.example.com/product-12.htm
    or
    http://www.example.com/product.....itle-here/

    if you have any solutions for this, please confirm me at:
    philipstel@gmail.com

    it will be very apreciated
    thx in advance !

    • Hiroshi says:

      @phil: This way

      Options +FollowSymLinks
      RewriteEngine on
      RewriteRule about/ about.php
      RewriteRule contact/ contact.php

      Add this to your .htaccess file and link about/ instead of about.php

Leave a Review

Incoming Searches

htaccess products, htaccess rewrite all mp3, htaccess rewrite css problem, what is NC L in htaccess, mode rewrite php to html, localhost htaccess url rewrite, mod rewrite /product, podcast mp3 count downloads rss php htaccess, php url rewrite problem for image javascript css, php url rewrite images, php url rewrite id to name, php url rewrite change id name, php script record htaccess user, mod rewrite hide id=1, PHP essential script, PHP and htaccess redirection: Rewriting yoursite com/user php?username=xyz to yoursite com/xyz, mod_rewrite mask file download, Mod_rewrite hide wp-content themes, php script essential, product detail url rewrite php, rewrite all php to html htaccess, url rewriting folder style, url rewriting css not loading via htaccess, url rewrite rule with img css js in htaccess, url rewrite on localhost in php, url rewrite htaccess js css, url rewrite folder css, switch php version on htaccess, Rewriting yoursite com/xyz to yoursite com/user php?username=xyz, Rewriting yoursite com/user php?username=xyz to yoursite com/xyz htaccess, Rewriting yoursite com/user php?username=xyz to yoursite com/xyz, rewriterule htaccess from mysql product, rewrite wamp absolute path, rewrite rule important, rewrite php database image, rewrite php class, rewrite image css javascript file, js css folder url rewrite, images not loading when I use htaccess, htaccess not showing directory rewrite on, htaccess not showing css and images, htaccess no rewrite on directories, htaccess mod rewrite no images css, htaccess jpg not displaying, htaccess hide jpg, htaccess do not rewrite full path, htaccess css loading problem, htacces rewrite folder change, htacces hide file path rewrite, how to update image in php, essential important, css problem in htaccess rewriteurl, css and js files not loading after url rewriting htaccess, contact php htaccess, htaccess not showing php, htaccess prod, htaccess product php?id=, images not loading htaccess, images not displaying in url rewriting in php, images is not showing while url rewriting in php, Images CSS problems when using htaccess, images are not displayed while using httaccess in php, htaccess url rewrite pictures not loading, htaccess url rewrite css problem, htaccess uri rewrite to share one ip address, htaccess RewriteRule ^products/([0-9] )/?$ product php?pid=$1 [NC L], htaccess rewriterule hide path, htaccess RewriteRule about, htaccess rewrite www if not localhost, htaccess rewrite theme url to css folder, htaccess rewrite operators, htaccess rewrite from database in php, change php to html htaccess