I wrote a post mid last year about protecting your site images from being hotlinked as I found a site that had word for word scrapped one of my blog posts, you can see the post here, and I really wanted to go back to this and run over a few more things the .htaccess file is useful for. I mainly use the .htaccess file to rewrite URIs for example http://mysite.com/site/pages/contact.php would be accessed by visiting http://mysite.com/contact. So the URI is being re-routed to my contact.php page.
You need to make sure if you want to rewrite URI’s that you have the mod_rewrite PHP module installed with your host. Most hosts will have this with the latest stable versions of PHP.
RewriteEngine On RewriteRule ^user/([a-z0-9]+)/$ /user.php?u=$1 [NC,L] RewriteRule ^analytics/$ /pages/analytics.php [NC,L] RewriteRule ^settings/$ /pages/settings.php [NC,L]
If you’ve just moved your site to a new domain and you want to transfer any visitors that visit your old site straight through to your new site just add this to your .htaccess file on your old domain.
RewriteEngine On RewriteRule ^(.*)$ http://www.yournewdomain.com/$1 [R=301,L]
If you have a server that only has php files on, you can lock it down so other file types can’t be executed
Options -ExecCGI AddHandler cgi-script .js .cgi .asp .jsp. htm .exe .sh
Probably the most common use for the .htaccess file is to setup custom error pages. this means that if you’re site throws a 404 not found error the user will see the page at http://yoursite.com/404.php
ErrorDocument 401 /401.php ErrorDocument 403 /403.php ErrorDocument 404 /404.php ErrorDocument 500 /500.php
This snippet of rewrite code will remove the www from your site domain url. For example wordpress automatically does this if you visit http://www.papermashup.com you’ll see that the url changes to http://papermashup.com.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Great work, Posted page link on my Page
https://plus.google.com/u/0/b/100003218692703397361/100003218692703397361
Hi, when I upload an .htaccess file, I cannot see it in my root folder. My site uses cpanel. What could be the problem?
where to put these code??? Do we have any common htaccess file or we have to create it…
i didn`t used htaccess… now i need to used it…
plz help me…
i want to give user name in the url like twitter and facebook did
http://www.facebook.com/mohanraaj
like this only i want to create url for user who login…
plz… i require this…
thanks in advance..
There’s another useful .htaccess thing and is that you can restrict the access to your website by IP.This is very useful if you need to block certain IP that’s bothering you but it can be very useful if you designed a website which can only be accesed from a certain country.
To use it,for example, to block a certain IP you would write:
#Block IP
order allow,deny
deny from 64.56.65.44
allow from all
To block a certain country or to allow only one country the easiest way is to go to this page http://www.countryipblocks.net/.On the right upper corner you would select the country (or the countries) and choose .htaccess deny and you would have the code for that.
You can block too a bot that is bugging you by blocking the agent or the referer.Two examples:
To block bot identified by larbin
#Block Agent
rewriteEngine on
rewriteCond %{HTTP_USER_AGENT} ^larbin
To block a bot that comes from a website named malicious web:
#Block Referer
rewriteEngine on
rewriteCond %{HTTP_REFERER} ^http(s)?://(www.)?maliciousweb.com*$
rewriteRule .* – [F]
rewriteRule .* – [F]
I saw your article its pretty good and informative and I am also using this technique to make .htaccess file for my site.
Mike you can and paste this code to your htaccess file..
#RewriteRule ^(.*)$ index.php?articles=$1
#RewriteRule ^(.*)/$ index.php?articles=$1
feel free to edit the code…..
thanks
I’m having trouble creating the friendly URL’s… I’m able to go to say, http://mikeutter.com/about/ (about.php), but it’s not pulling my CSS or any styling for that matter… thoughts?
Thanks!
bookmarked this.
oye chato eso estuvo muy bueno principalmente lo ultimo, es muy util ![]()
buen post
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
10 discussions around Useful .htaccess techniques