Showing posts with label htaccess. Show all posts
Showing posts with label htaccess. Show all posts

Wednesday, April 6, 2011

Tiny URL generating source code - PHP

Hi,

Tiny URL's are very popular when it comes to shortening a web URL. There are many third party tools such as bit.ly, tinyurl.com etc. But if you want to do it yourself, then you will find the entire source code and the logic of how it works in this post.

For this you need to keep in mind the following:
1. Providing an interface to the user to enter any URL and on submitting it a short URL is generated. The domain of the short URL will be in your server.
e.g.
if your website name is http://tiny.url, then the tiny urls should look something like, http://tiny.url/#####. Where "#####" is a tinyword which represent the actual URL which the user has entered.

2. You need to convert the above http://tiny.url/##### into its corresponding URL and redirect the page to there.

In my case instead of http://tiny.url/##### I will be using http://localhost/php/tinyurl/######, since I am running from my localhost. So lets start with the development.

Code and Explanation:

Thursday, March 10, 2011

Website Security

Hi,

Developing a website is not as easy as it may look or sound. Every time you write a code you need to keep two things in mind. Most important performance and security. In my earlier posts(also see compression and caching) I had discussed about performance. In this I will explain the most common security failures in a life of a programmer.

To start with, no one or nothing can teach you how to create a hack proof system. Here is the fact. Everything is hackable, every security can be breached. But what best you can do is to mislead the hacker. There is always someone somewhere who knows more than you, and can break into your system. So the best plan is to stick to few basic rules.

HTACCESS - Security

1. Never use actual URL's. 

Always try to mask the URL using htaccess. For example you have an image:
<img src="img/logo.jpg" /> now, instead of "img", keep the "logo.jpg" in some other directory with a name not easily identifiable, such as "assets/image_lib", where assets is a directory and images_lib the subdirectory. Now to make "images/logo.jpg" point to "assets/image_lib/logo.jpg", create a .htaccess in root directory with the following URL rewrite:

RewriteEngine on
RewriteRule ^img/(.*).(jpg|png|gif|jpeg)$ assets/image_lib/$1.$2 [L]

2. Forbid Directory

Even if someone gets to know the actual directory, prevent from seeing the content of the directory, for this create a new .htaccess file inside the directory whose access you want to forbid with the following:

Wednesday, March 2, 2011

Adding a WWW at the beginning of a URL automatically

Hi,

If you want to add a "www" infront of your web URL automatically, here is how you can achieve it. For this you will need a .htaccess file located at the root directory of your website with the following:


RewriteEngine on
Options FollowSymlinks
rewritecond %{http_host} ^my_website.com [nc]
rewriterule ^(.*)$ http://www.my_website.com/$1 [r=301,nc]

"my_website" is the name of your website.

Similarly if you want to remove a "www" from the URL:


RewriteEngine on
Options FollowSymlinks
rewritecond %{http_host} ^www.my_website.com [nc]
rewriterule ^(.*)$ http://my_website.com/$1 [r=301,nc]

Hope it helps

Tuesday, March 1, 2011

Windows Authentication - Hiding URL's from search engines and users

Hi,

If you want to put windows authentication to your website, it can be achieved by simply using 2 files. One is the .htaccess and the other is the .htpasswd. The idea of using this is to prevent certain sections of your website to be accessed by the end user or from being crawled by search engines. For example, if you have a admin in a separate folder but in the same root directory of the site, then obviously you do no want your admin pages to get indexed by search engine. This is when windows authentication comes in play.

Whenever search engines try to crawl your website and finds a windows authentication to a page, the URL does not get indexed. And even if by chance any user gets to know such an URL, they cannot see the page unless they have entered the correct credentials. I am using localhost for this demonstration. Here is how an authentication box looks like:


Here is how you can achieve this. Create or modify your .htaccess file and add in the following: