Webmaster Key - Discussion Forums


Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2012, 10:09:09 PM

Login with username, password and session length
Welceome to Forums!

Important information for guests and new members:

In order to understand the full benefits of becoming an active member of this forum, please review the following information on guest and new member restrictions. These forum changes have been prompted by an overwhelming and unreasonable amount of bot postings and incoherent guest spam messages. We wish to prevent these events from happening in the future and make our community a more comfortable place for all of our members.

For guests:

Guests are not allowed to open new topics, polls, or posts attachments.
If you wish to open up new discussions on this forum, we encourage you to register.

For new members:

New members with less than five posts are not allowed to modify additional profile information such as avatars, contact information, biographies, and signatures. However, new members are encouraged to post their own topics or reply to topics initiated by other members. Become active on the forums and 5 posts should be an easy task!

We are a diverse community with members from all over the world. We encourage new ideas and interesting conversation. Do not be afraid to post webmaster/computer-related questions or problems, as our active members are always willing to help when they are able. Interested? Join us.

+ Webmaster Key Forums
|-+ General Discussion
| |-+ Tech Corner
| | |-+ HOWTOs Library
| | | |-+ .htaccess tips
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print
Author Topic: .htaccess tips  (Read 16076 times)
Choplin
Limited Member

Posts: 3



« on: December 18, 2006, 02:12:36 PM »

Below I show some common uses of .htaccess which I use myself on many sites. Many of you will find no new information here, but hopefully it will help some!


Code:
Options -Indexes This causes the server not to list the contents of directories (when there is no index). This keeps people from typing in http://www.domainexample.net/images/ (or any other directory) and getting a list of the files.


Code:
AddType application/x-httpd-php .php .htmThis code tells your server to process PHP code in files with the .htm extension. Some servers allready do this by default, but many do not. You can also add .html to the end (or any other extension) if you like. This will keep you from having to rename files to php if you add scripts in later, and also I just happen to like it more


Code:
ErrorDocument 400 /errors/400.htm
ErrorDocument 401 /errors/401.htm
ErrorDocument 403 /errors/403.htm
ErrorDocument 404 /errors/404.htm
ErrorDocument 500 /errors/500.htm The control panel of many hosts let you set the custom error pages, but doing it directly is just as easy. I use these 5 common ones. Just create your pretty error pages, and point to them in .htaccess.


Code:
 redirectPermanent /somepage.htm http://www.domainexample.net/index.htmJust a basic redirect example here. For when you get rid of a page and want all links to it to just point to the index.


Code:
redirectMatch 301 ^/old_directory/(.*) http://www.domainexample.net/index.htmHere we get a little fancier. This is for when you remove an entire directory. It will make any link to that directory (including all pages and sub-pages in it) go back to your main index.


Code:
redirectMatch 301 ^/subsite/(.*) http://www.domainexample.net/$1
redirectMatch 301 ^/subsite http://www.domainexample.net/$1Say you have a sub-site on your domain.. in the 'subsite' directory, and you moved it to its own domain. These two redirects will take all possible link combinations and redirect them to the new domain. Even if the link is like this : /subsite/1/2/3/4/index.php it will redirect to www.domainexample.net/1/2/3/4/index.php. The two lines give you better flexibility. It handles all combinations of trailing slashes, directories/files, and www. prefixes.


Code:
Options +FollowSymLinks
RewriteEngine onThis code gets the server ready for the next few things we will do


Code:
RewriteCond %{HTTP_HOST} ^example.(.*)
RewriteRule ^(.*)$ http://www.domainexample.net/$1 [R=301,L] This bit of code is quite usefull. It will take any links that do not have www in them, and add it to the URL. This means no matter how they link to you, your full domain (with the www) will get the backlink.


Code:
RewriteCond %{HTTP_REFERER} !^http://www.domainexample.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domainexample.net$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.domainexample.net/index.htm [R,NC]This is basic hot-link protection code. I did have two other lines added to it (to take care of the times when the link does not contain the www) but if you use the last tip, you do not have to worry about it. Just make sure to put them in order (so the www gets added before the hot-link protection). This will only allow hot-linking from your site. If you want to add another site, just add the appropriate lines.

Now if you want to sometimes allow hot-linking, here is a good method. Create a directory called 'share' (or whatever) and stick in a new .htaccess file with only this line:


Code:
RewriteEngine offThis will counter-act the global .htaccess, and allow hot-linking from whatever directory you stick it in (and sub-directories)
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #1 on: December 18, 2006, 02:53:51 PM »

Hi Choplin,

that's all good stuff but to make it accessible to newbies you need to explain what .htaccess is, what kind of server can use it and how to add it to their web sites.

Thanks all the same  Smiley
Report to moderator   Logged

SensoVision
Administrator
Veteran
*****
Posts: 5 857


I'm proud user of Debian GNU/Linux OS


WWW
« Reply #2 on: December 18, 2006, 06:45:08 PM »

Welcome to WKey forums Choplin! And thanks for good article! I'm sure that people who aware of .htaccess and doesn't know the exact way it works this post would be very useful.

Hope you don't mind I've moved your thread to HOWTO Library board so it would get higher exposure.

I'm looking forward to see more good posts from you  Wink
« Last Edit: December 18, 2006, 06:48:54 PM by SensoVision » Report to moderator   Logged

Denis
johnhask
Contributor
Full Member
***
Posts: 182


« Reply #3 on: October 13, 2007, 04:27:57 PM »

And for those visitors/bots that we wish to block:

<IfModule mod_access.c>
   <FilesMatch ".*">
      Order Allow,Deny
      Allow from all
      Deny from 64.127.103.0
      Deny from 58.65.240.0
   </FilesMatch>
</IfModule>

You can shorten the ip range to include the entire block (64.)
Report to moderator   Logged

Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #4 on: October 14, 2007, 10:07:11 AM »

For people that don't know what .htaccess is, it's a service available on Apache servers that run on Linux-based web hosting. It is a text file called .htaccess (no file extension) containing a list of commands. You upload this file in a folder above/containing any of the files and folders you want to effect (usually in the root of your web site).

What it does is control how URLs are delivered on your site. It is not required but very useful for redirecting web browsers invisibly. The downside is that it is very difficult to debug if it doesn't work as you expected because you don't get any error messages, it either works or does nothing.

For example, in most cases you don't need Options +FollowSymLinks but on some servers you do. The Options directive is used to override the standard settings of the server. Also, mod-rewrite must have been installed when the server was set up. If not you could ask your server admin to enable it for you.

The examples given in previous posts will give you a flavor of what can be done.
« Last Edit: October 15, 2007, 12:48:51 AM by Andy » Report to moderator   Logged

donecweb
Administrator
Veteran
*****
Posts: 1 303


DonecWeb


« Reply #5 on: October 14, 2007, 11:18:28 PM »

Andy, that was a good explanation except for the "You upload this file in a folder below any of the files you want to effect" Shouldn't that be in a folder above/containing any of the files and folders you want to effect. I don't know much about .htaccess and could be wrong but I have seen other post about it and this is what I thought I understood.
Report to moderator   Logged

DonEc Web

Links and accurate information provide the best answer, while garbage in provides garbage out.
Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #6 on: October 15, 2007, 12:47:51 AM »

That's what I meant to say, I'll edit my post. Thanks for pointing this out.
Report to moderator   Logged

Pages: [1] Go Up Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print 
+ Webmaster Key Forums
|-+ General Discussion
| |-+ Tech Corner
| | |-+ HOWTOs Library
| | | |-+ .htaccess tips

Jump to:  
« previous next »


Our Partners
RelmaxTOP Ranking System Web Hosting RelmaxTOP Ranking System
Staff Sites
12Noon[12Noon Gallery] Andy[Urgentclick]
Tamuril[Tamuril's Digital Art Exhibit] Sensovision
Powered by MySQL Powered by PHP We are hosted by Relmax Inc. |Our Privacy Policy | Sitemap
Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Forum design by Tamuril © 2005.
Valid XHTML 1.0! Valid CSS!