Social Networking/E-Commerce Web Design & Development

rss

PHP / mySQL / xml / xHTML / CSS / DOM - By Dennis Plucinik

How to Figure Out .htaccess Quickly

Being that this is something that I just had to figure out today, I thought I’d post up my notes which I found helpful on this topic. Let me know if you think anything else should be added.

.htaccess files are/have/etc:
created in notepad
have no file name or different extension - just .htaccess (not file.htaccess)
chmod to 644 (makes it usable by the server but prevents browser use)
.htaccess is for Apache not NT servers
.htaccess files effect the directory they are in and all sub-directories
.htaccess in child folders overrule .htaccess in it’s parent’s folders

Things .htaccess can do:
- mod_rewrite
- password protection
- enabling SSI
- blocking users by IP
- blocking users/ sites by referrer
- blocking bad bots and site rippers
- change your default directory page
- redirects
- prevent viewing of .htaccess
- adding MIMI types
- prevent hot linking of your images and other files
- prevent directory listing

It’s use is pretty straightforward. You just have to learn the syntax. For example, here’s how you…

Create a redirect:

1) Open notepad
2) Type this:

Redirect /olddirectory/oldfile.html http://yoursite.com/newdirect

3) Save as .htaccess (make sure to change ’save as’ type to ‘All Files’)

Additional notes on the redirect function:
- the code should be on one line
- it can also be used to redirect a whole directory…

Ex:

Redirect /olddirectory http://yoursite.com/newdirectory/

Prevent directory listings:

IndexIgnore *

- put this in the images folder to prevent any files from being listed

IndexIgnore *.gif *.jpg

- put this in a folder to allow all but gif and jpeg files to be listed

Options +Indexes

- put this in any folder to allow access to that folder’s files

Other helpful htaccess links:

http://www.javascriptkit.com/howto/htaccess.shtml
- pretty nice intro tutorial

http://httpd.apache.org/docs/1.3/mod/directives.html
- official list of complete Apache server directives for .htaccess

http://en.wikipedia.org/wiki/Htaccess
- quick overview with decent links

http://www.thejackol.com/htaccess-cheatsheet/
- a pretty cool quicklist

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google]

8 Comments, Comment or Ping

  1. gravatar

    Just a small note: if one is redirecting, then it’s a good idea to add a 301 status code, showing that the redirect is permanent (which in most cases it is), so

    Redirect 301 /olddirectory http://yoursite.com/newdirectory/

  2. gravatar

    Agreed. Unfortunately I broke that little rule recently… a lot. This blog was hosted on the Wordpress server and when I moved here, I exported the posts from there and imported them here but there was no way to 301 the existing pages here.

    So I deleted all the posts from the Wordpress server and put up a new post that says “This site moved here: [link]”

    I know it’s bad. There has to be some way to do it. I can’t imagine EVERYONE who moves from the Wordpress server to their own as their blog grows has to lose all their built up ranking.

  3. gravatar

    Dennis
    You could have used one line of mod_rewrite rules (with the rewrite engine on, allows you to use regular expressions) and redirected every post on the old server to the version on your new server.

    Something like this:

    RewriteEngine on
    RewriteRule ^(.*)$ http://dennisplucinik..com/$1 [R=301,L]

    Then once Google et. al. have caught on, you can delete the old site.

    Haven’t had time to check the above rule, but this or something similar would do the trick.

  4. gravatar

    Oh, and I forgot to mention the Permanent Redirect Plugin (for WordPress hosted blogs):
    http://fucoder.com/code/permalink-redirect/

  5. gravatar

    Very helpful links, thanks for the tips. I tried to modify my.htaccess once and someone in #Linux gave me a link to the mod_rewrite page in the Apache web site and asked me to read and understand it myself. [sarcastic]That was very helpful[/sarcastic].

  6. gravatar

    Johno, I’m just getting familiar with Wordpress and I know very well how inportant 301 redirects are. I suppose I should have looked into it a little more. Suffice it to say that since having moved form Wordpress to my own server, the traffic here has increased 5 - 10 fold. I’ll most likely eventually delete the old blog entirely so Goggle gets it out of it’s index.

    Lorna, that’s typical of a cocky linux user lol (j/k) - I feel like sometimes a quick run through tutorial/brief like this is better than the long drawn out article. At least for me anyways.

  7. gravatar

    That’s great news about the increase in traffic!
    Yes, it might be time to delete the old blog. Google does not take kindly to duplicate content–it’s perceived as a form of spam content. So, upon deleting the old blog, you may even see a further jump in traffic.

    ps: the RSS feed in the footer has the wrong link. Not a big problem, because I could subsrcibe through the link in the header.

  8. gravatar

    That is strange. I’ll fix that right now, thanks for pointing it out.

Reply to “How to Figure Out .htaccess Quickly”