Hello friends !!
Today i want to give you some more .htaccess tips and tricks for url rewriting in PHP.
In my last post, i was show you basick url rewriting. In this post i will write some more .htaccess tricks for you.
Control Access
Being able to control access to certain areas of your server can be very useful. The following example demonstrates how to only allow access from those connecting from a 192.168.0 LAN IP pool. This could be easily modified to only allow access from a single remote IP address or addresses.
deny from all
allow from 192.168.0.0/24
Shorter URLs
Shorter URLs are beneficial, as visitors that persist in typing full URLs won’t have to type as much, and they’re more memorable. Do they benefit SEO, even though the full URL contains the same keywords? I don’t know, maybe someone can tell me.
This example will rewrite a page requested as http://xyz.com/files/code/apache.zip to http://xyz.com/download.php?type=code&file=apache.
RewriteEngine on
RewriteRule ^files/(.+)/(.+).zip download.php?type=$1&file=$2 [nc]
Custom Error Documents
Creating custom documents gives your site a more professional look, as not only are you providing a ‘net’ to catch unsuspecting visitors when they follow a bad link and such like, but they also allow you to customise the style of the page so you can maintain your basic site design by adding HTML.
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
Hide and Deny Files
Hiding and denying access to files is crucial to servers that have sensitive data held within files that may be accessible via the website(s) on it. The following example demonstrates how to prevent acces to any files ending with .log – and is case insensitive (i.e. .LoG / .lOG / .loG).
Deny from all
Satisfy All
Prevent Hotlinking
Preventing hotlinking can reduce bandwidth, by disallowing other websites from displaying images hosted on your server. The following rule basically says that if the referer is NOT xyz.com, run the following rule. The rule (on the next line) says that if the request is for a .gif, .jpg or.png then redirect the visitor to http://xyz.com/img/hotlink_f_o.png. I’ll leave you to work out what the ‘f_o’ stands for.
# no hot-linking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?xyz\.com/ [nc]
RewriteRule .*\.(gif|jpg|png)$ http://xyz/img/hotlink_f_o.png [nc]
Making Other Filetypes Executable
Ever wanted to make your site look like it runs off a new language such as .w00t files? Well you can with .htaccess! Adding this neat one-liner, you can request .w00t files, which will be served and interpreted as .php type files.
Require SSL
Sometimes you will require an SSL connection. This following snippet will do just that!
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq “domain.tld”
ErrorDocument 403 https://domain.tld# require SSL without mod_ssl
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Force Media Downloads
Sometimes, when clicking on media files, the browser wants to play or stream it directly from itself. Using the following rules, media files (.avi/.mpg/.wmv/.mp3 in this example) will provide a download dialog box instead.
AddType application/octet-stream .mpg
AddType application/octet-stream .wmv
AddType application/octet-stream .mp3
Ban Selected User Agents
The following provides some examples for blocking requests to your server from certain user agents.
RewriteCond %{HTTP_USER_AGENT} ^Java.* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Microsoft.URL [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^MSFrontPage [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline.Explorer [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[Ww]eb[Bb]andit [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus [NC]
RewriteRule ^.*$ – [F]
I think these tricks are become helpful to all of you.
Thanks !!


URL rewriting Tips and Tricks in .htaccess…
List of .htaccess tips and tricks including shorter URLS, custom error documents, hotlink prevention and more….
[...] Trucos para rescritura de URLs PHP en .htaccess (en inglés). vía: php genious [...]
[...] URL rewriting Tips and Tricks in .htaccess – PHP Genious [...]
Hey This is great info, am into all this… cool sh!t man