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
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 https://localhost/product-5.html calls product.php?id=5 automatically
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1
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.