.htaccess redirect generator

Discussion in 'Web Development and Programming' started by Brandon, Mar 24, 2010.

  1. Brandon

    Brandon Regular Member

    Joined:
    Jun 1, 2009
    Messages:
    6,602
    Likes Received:
    1,706
    Location:
    Topeka, Kansas
    First Name:
    Brandon
    I'm always getting asked how to setup htaccess files and I ran across this page and thought it was pretty slick, so I thought I would share it with you good folks.

    Free .htaccess Redirect Generator

    Here is a simple cheatsheet for the .htaccess file:
    Enable Directory Browsing
    Options +Indexes
    ## block a few types of files from showing
    IndexIgnore *.wmv *.mp4 *.avi
    Disable Directory Browsing
    Options All -Indexes
    Customize Error Messages
    ErrorDocument 403 /forbidden.html
    ErrorDocument 404 /notfound.html
    ErrorDocument 500 /servererror.html
    Get SSI working with HTML/SHTML
    AddType text/html .html
    AddType text/html .shtml
    AddHandler server-parsed .html
    AddHandler server-parsed .shtml
    # AddHandler server-parsed .htm
    Change Default Page (order is followed!)
    DirectoryIndex myhome.htm index.htm index.php
    Block Users from accessing the site
    <limit GET POST PUT>
    order deny,allow
    deny from 202.54.122.33
    deny from 8.70.44.53
    deny from .spammers.com
    allow from all
    </limit>
    Allow only LAN users
    order deny,allow
    deny from all
    allow from 192.168.0.0/24
    Redirect Visitors to New Page/Directory
    Redirect oldpage.html http://www.domainname.com/newpage.html
    Redirect /olddir http://www.domainname.com/newdir/
    Block site from specific referrers
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} site-to-block\.com [NC]
    RewriteCond %{HTTP_REFERER} site-to-block-2\.com [NC]
    RewriteRule .* - [F]
    Block Hot Linking/Bandwidth hogging
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
    RewriteRule \.(gif|jpg)$ - [F]
    Want to show a “Stealing is Bad” message too?
    Add this below the Hot Link Blocking code:
    RewriteRule \.(gif|jpg)$ http://www.mydomain.com/dontsteal.gif [R,L]
    Stop .htaccess (or any other file) from being viewed
    <files file-name>
    order allow,deny
    deny from all
    </files>
    Avoid the 500 Error
    # Avoid 500 error by passing charset
    AddDefaultCharset utf-8
    Grant CGI Access in a directory
    Options +ExecCGI
    AddHandler cgi-script cgi pl
    # To enable all scripts in a directory use the following
    # SetHandler cgi-script
    Password Protecting Directories
    Use the .htaccess Password Generator and follow the brief instructions!
    Change Script Extensions
    AddType application/x-httpd-php .gne
    gne will now be treated as PHP files! Similarly, x-httpd-cgi for CGI files, etc.
    Use MD5 Digests
    Performance may take a hit but if thats not a problem, this is a nice option to turn on.
    ContentDigest On
    The CheckSpelling Directive
    From Jens Meiert: CheckSpelling corrects simple spelling errors (for example, if someone forgets a letter or if any character is just wrong). Just add CheckSpelling On to your htaccess file.
    The ContentDigest Directive
    As the Apache core features documentation says: “This directive enables the generation of Content-MD5 headers as defined in RFC1864 respectively RFC2068. The Content-MD5 header provides an end-to-end message integrity check (MIC) of the entity-body. A proxy or client may check this header for detecting accidental modification of the entity-body in transit.
    Note that this can cause performance problems on your server since the message digest is computed on every request (the values are not cached). Content-MD5 is only sent for documents served by the core, and not by any module. For example, SSI documents, output from CGI scripts, and byte range responses do not have this header.”
    To turn this on, just add ContentDigest On.
    Enable Gzip – Save Bandwidth
    # BEGIN GZIP
    <ifmodule mod_deflate.c>
    # Combine the below two lines - I've split it up for presentation
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css
    application/x-javascript application/javascript
    </ifmodule>
    # END GZIP
    Turn off magic_quotes_gpc
    # Only if you use PHP
    <ifmodule mod_php4.c>
    php_flag magic_quotes_gpc off
    </ifmodule>
    Set an Expires header and enable Cache-Control
    <ifmodule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 seconds"
    ExpiresByType text/html "access plus 7200 seconds"
    ExpiresByType image/gif "access plus 518400 seconds"
    ExpiresByType image/jpeg "access plus 518400 seconds"
    ExpiresByType image/png "access plus 518400 seconds"
    ExpiresByType text/css "access plus 518400 seconds"
    ExpiresByType text/javascript "access plus 216000 seconds"
    ExpiresByType application/x-javascript "access plus 216000 seconds"
    </ifmodule>

    <ifmodule mod_headers.c>
    # Cache specified files for 6 days
    <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
    Header set Cache-Control "max-age=518400, public"
    </filesmatch>
    # Cache HTML files for a couple hours
    <filesmatch "\.(html|htm)$">
    Header set Cache-Control "max-age=7200, private, must-revalidate"
    </filesmatch>
    # Cache PDFs for a day
    <filesmatch "\.(pdf)$">
    Header set Cache-Control "max-age=86400, public"
    </filesmatch>
    # Cache Javascripts for 2.5 days
    <filesmatch "\.(js)$">
    Header set Cache-Control "max-age=216000, private"
    </filesmatch>
    </ifmodule>
     
  2. Bundy

    Bundy Admin Talk Staff

    Joined:
    Apr 19, 2005
    Messages:
    842
    Likes Received:
    36
    Location:
    Boston
    Great site!
     
  3. Brandon

    Brandon Regular Member

    Joined:
    Jun 1, 2009
    Messages:
    6,602
    Likes Received:
    1,706
    Location:
    Topeka, Kansas
    First Name:
    Brandon
    ya I thought so too :)
     
  4. kneel

    kneel Regular Member

    Joined:
    Jun 25, 2009
    Messages:
    612
    Likes Received:
    16
    oh man....i've been looking for sumthing like that....nice find...thanks for posting!!

    So...if I wanted to make my vb forum the main page you come to when you go www.mysite.com i could use that rewrite function to change it to www.mysite.com/forum? then when people would type in www.mysite.com it would take them right to the forum...correct?

    I've been thinking about ditching one of my vbadvanced I use and just having the forum as the main page..
     
  5. MadMikeyB

    MadMikeyB Regular Member

    Joined:
    Aug 16, 2009
    Messages:
    158
    Likes Received:
    0
    To do that, you just need to put this in your htaccess file;

    Code:
    RewriteRule ^$ /forum/index.php
     
  6. kneel

    kneel Regular Member

    Joined:
    Jun 25, 2009
    Messages:
    612
    Likes Received:
    16
    thank you!

    I figured it was simple...but i never really messed with it.
     
  7. kneel

    kneel Regular Member

    Joined:
    Jun 25, 2009
    Messages:
    612
    Likes Received:
    16
    just a follow up on this....it work flawlessly...(<---is that even CLOSE to being spelled right???/ LOL...idk...)


    But anyways.....just wanted to say thanks....
     
  8. kneel

    kneel Regular Member

    Joined:
    Jun 25, 2009
    Messages:
    612
    Likes Received:
    16
    why is this not working now?

    I reinstalled EVERYTHING...all i want to do is make mysite.com go to mysite.com/forum?

    I put that in there and its not working...what am I doing wrong?
     
  9. Brandon

    Brandon Regular Member

    Joined:
    Jun 1, 2009
    Messages:
    6,602
    Likes Received:
    1,706
    Location:
    Topeka, Kansas
    First Name:
    Brandon
    I added some more info to the first post
    hope it's helpful :)
     
  10. Cerberus

    Cerberus Admin Talk Staff

    Joined:
    May 3, 2009
    Messages:
    1,031
    Likes Received:
    500
    This is nice, but always though one should do this themselves, if you do it forces you to learn and understand. Its good knowledge to know if your going to own a website. But thats just my opinion
     
  11. Brandon

    Brandon Regular Member

    Joined:
    Jun 1, 2009
    Messages:
    6,602
    Likes Received:
    1,706
    Location:
    Topeka, Kansas
    First Name:
    Brandon
    That goes with everything ;)
    but this is handy if you only need a rule here and there :readup:
    plus, this thread gets lot's of search engine hits..lol :partytime:
     
  12. kneel

    kneel Regular Member

    Joined:
    Jun 25, 2009
    Messages:
    612
    Likes Received:
    16
    This is still a great thread!!!
     
  13. redkip

    redkip Regular Member

    Joined:
    Nov 30, 2010
    Messages:
    1
    Likes Received:
    0

Share This Page