How to Rewrite urls with .htaccess
I have received many requests from my readers to write some thing on .htaccess so today I am going to explain you how to rewrite URLs with .htaccess. Hypertext access is shortly called as htaccess, it rewrite URLs for directory in which file placed it manage that directory only, we can make SEO friendly URLs and boost your search engine ranking.
Write your htaccess code inside these tags:
<IfModule mod_rewrite.c> RewriteEngine on #Write your conditions here# </IfModule>
Example Pretty URLs
https://www.phpgang.com/profile.php?user=facebook
to
https://www.phpgang.com/facebook
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1 </IfModule>
In your profile.php file you get user value in $_GET[‘user’] in both condition.
Remove file extension .php or .html
https://www.phpgang.com/home.php
to
https://www.phpgang.com/home
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d # rewrite condition if it is not directory RewriteCond %{REQUEST_FILENAME}\.php -f # rewrite condition if it file with php extension RewriteRule ^(.*)$ $1.php </IfModule>
This example can remove .php extension and show you only home.
Redirect old file to new file path
Redirect https://www.phpgang.com/olddirectory/old.php https://www.phpgang.com/newdirectory/new.php
This redirect can move old page to new page.
Listing enable and disable
# allows to display directory listing Options +Indexes # disable directory listing Options -Indexes OR IndexIgnore *
Error Page
errorDocument 400 http://demo.phpgang.com/404.html errorDocument 401 http://demo.phpgang.com/404.html errorDocument 404 http://demo.phpgang.com/404.html errorDocument 500 http://demo.phpgang.com/404.html
This code will redirect when an 400, 401, 404 or 500 error occurred it opens 404.html file.
Cache static files
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> Header set Cache-Control "max-age=604800, public"
Very useful feature in .htaccess is caching of static files above example cache all images for 1 week this will increase your website’s speed.
Redirect to www
phpgang.com
to
www.phpgang.com
If you type your website name in browser without www it automatically add www before your domain name.
RewriteEngine On RewriteCond %(HTTP_HOST) ! ^www.phpgang.com$ [NC] #NC - represents not case sensitive. RewriteRule ^(.*)$ https://www.phpgang.com/$1 [L, R=301]
^ – starting
.* – one or more characters.
$ – ending
L – if rule matches, don’t process any other rewrite rules below this one.
R=302 – redirect code
There is many more about htaccess we will discuss in future.
I hope you like this tutorial if you have any suggestions and improvement please feel free to comment bellow.
Tutorial Categories:
Dear Php Gang Team
So Many Thanks for your Post about .htaccess
Now i got .htaccess Stand For
Hypertext access is shortly called as htaccess,
i have some confusion Can u Please Tell me what is $1
Can u Please Tell me Which Code i have to Use Like this URL Example you Mention in top
https://www.phpgang.com/profile.php?user=facebook
to
https://www.phpgang.com/facebook
Can PhpGang upload a Source Code (.htaccess+ .php file like profile.php?user=facebook)
Please guide me. i m trying to read this Article and understanding but having some confusin
Once again PhpGang Love you all Team do good work
$1 is the parameter user value which you will receive in your profile.php file This is the php file.
Bux Sir. Can u Please tell me whihc code i should use. i m trying this one but not getting result
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # rewrite condition if it is not directory
RewriteCond %{REQUEST_FILENAME}.php -f # rewrite condition if it file with php extension
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteRule ^/?test/([0-9]+)$ /test.php?id=$1
This is my .htaccess Code
every thing working file but I cannot access the parameter value using $_GET[‘id’], it;s showing the error (Array ( [type] => 8 [message] => Undefined index: id [file] => /home/…./test.php [line] => 8 ))
Please Help me sir.
Thanks.
Dear Php Gang Team
i m Trying to learn .htaccess since morning. i m getting this error on my localhost
=====
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
======
Please help me
Might be this module, is inactive on your server add following line in httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
and un-comment this line in httpd.conf
RewriteEngine on
i am also suffer with this error, when i used this code on my localhost.
I suggest to use this on hosted sites not on localhost.
Here is the solution if you are on windows
in your httpd.conf Change this line
#LoadModule rewrite_module modules/mod_rewrite.so
to
LoadModule rewrite_module modules/mod_rewrite.so
and enable .htaccess by changing
AllowOverride None
to
AllowOverride All
I hope it works.
i used this code local & live but the issue is remain same 🙁
Share your htaccess code please
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # rewrite condition if it is not directory
RewriteCond %{REQUEST_FILENAME}.php -f # rewrite condition if it file with php extension
RewriteRule ^(.*)$ $1.php
Where can i find httpd.conf file?
If you are using wamp server your httpd.conf should be on this path: C:/wamp/bin/apache/apache2.2.22/conf/httpd.conf
If you are using wamp server your httpd.conf should be on this path: C:/xampp/apache/conf/httpd.conf
Dear Php Gang Team,
Please it a request, Can u Please upload a demo and download file, so may we can test on live hosting.
Already sent request to PhpGang Team, But not got reply yet.
Please Sir
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
i am using this code… but my url is not change… please help me.
There is a mistake in you code please you this code
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ test.php?q=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ test.php?q=$1
Why adding =”” in start tag
sir please can you upload whole file n demo file.
hi i am writing this code , This is not working, Only first line code is working, Second line code is not working, Kindly help
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*).php$ /company/?tag=$1
RewriteRule ^([^/]*).php$ /product/?tag=$1
Sir, rewrite rule is different for dot com or other dots(like .in,.org)
Same for every domain extension.
Awesome 🙂