Results 1 to 4 of 4

Thread: Ultimate .htaccess Codes - Adding Domains - All 57 Apache Codes - Subdomain Pointing

  1. #1

    Join Date
    Jul 2005
    Location
    USA
    Posts
    119
    Rep Power
    12

    Lightbulb Ultimate .htaccess Codes - Adding Domains - All 57 Apache Codes - Subdomain Pointing

    Heres my list of the ultimate htaccess code snippets and examples that I use all the time. I tried to keep them extremely minimalistic.

    Each code snippet has been copied from htaccesselite. Additional and detailed info on each htaccess code snippet can be found at htaccessElite

    Most of these snippets can be used with a Files or Filesmatch directive to only apply to certain files.





    Make any file be a certain filetype (regardless of name or extension)
    Code:
    #Makes image.gif, blah.html, index.cgi all act as php
    ForceType application/x-httpd-php




    Authentication Magic

    Require password for 1 file:
    Code:
    <Files login.php>
    AuthName "Prompt"
    AuthType Basic
    AuthUserFile /home/askapache.com/.htpasswd
    Require valid-user
    </Files>
    Protect multiple files:
    Code:
    <FilesMatch "^(exec|env|doit|phpinfo|w)*$">
    AuthName "Development"
    AuthUserFile /.htpasswd
    AuthType basic
    Require valid-user
    </FilesMatch>
    Example uses of the Allow Directive:
    Code:
    # A (partial) domain-name 
    Allow from 10.1.0.0/255.255.0.0
    
    # Full IP address
    Allow from 10.1.2.3
    
    # More than 1 full IP address
    Allow from 192.168.1.104 192.168.1.205
    
    # Partial IP addresses 
    # first 1 to 3 bytes of IP, for subnet restriction.
    Allow from 10.1
    Allow from 10 172.20 192.168.2
    
    # network/netmask pair
    Allow from 10.1.0.0/255.255.0.0
    
    # network/nnn CIDR specification
    Allow from 10.1.0.0/16
    
    # IPv6 addresses and subnets
    Allow from 2001:db8::a00:20ff:fea7:ccea
    Allow from 2001:db8::a00:20ff:fea7:ccea/10
    Using visitor dependent environment variables:
    Code:
    SetEnvIf User-Agent ^KnockKnock/2\.0 let_me_in
    Order Deny,Allow
    Deny from all
    Allow from env=let_me_in

    Allow from apache.org but deny from foo.apache.org
    Code:
    Order Allow,Deny
    Allow from apache.org
    Deny from foo.apache.org
    Allow from IP address with no password prompt, and also allow from non-Ip address with password prompt:
    Code:
    AuthUserFile /home/www/site1-passwd
    AuthType Basic
    AuthName MySite
    Require valid-user
    Allow from 172.17.10
    Satisfy Any
    block access to files during certain hours of the day
    Code:
    # If the hour is 16 (4 PM) Then deny all access 
    RewriteCond %{TIME_HOUR} ^16$    
    RewriteRule ^.*$ - [F,L]




    Redirect non-https requests to https server fixing double-login problem and ensuring that htpasswd authorization can only be entered using HTTPS
    Code:
    SSLOptions +StrictRequire
    SSLRequireSSL
    SSLRequire %{HTTP_HOST} eq "google.com"
    ErrorDocument 403 https://google.com




    SEO Friendly redirects for bad/old links and moved links
    For single moved file
    Code:
    Redirect 301 /d/file.html http://www.htaccesselite.com/r/file.html
    For multiple files like a blog/this.php?gh
    Code:
    RedirectMatch 301 /blog(.*) http://www.askapache.com/$1
    different domain name
    Code:
    Redirect 301 / http://www.newdomain.com




    Require the www
    Code:
    RewriteCond %{HTTP_HOST} !^www\.example\.com$
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]




    Redirect everyone to different site except 1 IP address (useful for web-development)
    Code:
    ErrorDocument 403 http://www.someothersite.com
    Order deny,allow
    Deny from all
    Allow from 24.33.65.6




    CHMOD your files
    chmod .htpasswd files 640
    chmod .htaccess files 644
    chmod php files 600
    chmod files that you really don't want people to see as 400
    NEVER chmod 777, if something requires write access use 766





    Variable (mod_env) Magic
    Set the Timezone of the server:
    Code:
    SetEnv TZ America/Indianapolis
    Set the Server Administrator Email:
    SetEnv SERVER_ADMIN webmaster@htaccesselite.com





    Turn off the ServerSignature
    Code:
    ServerSignature Off




    Add a "en" language tag and "text/html; UTF-8" headers without meta tags
    Code:
    AddDefaultCharset UTF-8
    # Or AddType 'text/html; charset=UTF-8' html
    DefaultLanguage en-US




    Use a custom php.ini

    Detailed instructions for doing this whether you are using php as a cgi or the apache module mod_php





    Securing directories: Remove the ability to execute scripts

    Heres a couple different ways I do it
    Code:
    AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi 
    Options -ExecCGI
    This is cool, you are basically categorizing all those files that end in those extensions so that they fall under the jurisdiction of the -ExecCGI command, which also means -FollowSymLinks (and the opposite is also true, +ExecCGI also turns on +FollowSymLinks)





    Only allow GET and PUT request methods to your server.

    Code:
    Options -ExecCGI -Indexes -All 
    RewriteEngine on 
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) RewriteRule .* - [F]




    Processing All gif files to be processed through a cgi script
    Code:
    Action image/gif /cgi-bin/filter.cgi




    Process request/file depending on the request method
    Code:
    Script PUT /cgi-bin/upload.cgi




    Force Files to download, not be displayed in browser

    Code:
    AddType application/octet-stream .avi 
    AddType application/octet-stream .mpg
    Then in your HTML you could just link directly to the file..
    Code:
    <a href="/movies/mov1.avi">Download Movie1</a>
    And then you will get a pop-up box asking whether you want to save the file or open it.





    Show the source of dynamic files

    If you'd rather have .pl, .py, or .cgi files displayed in the browser as source rather than be executed as scripts, simply create a .htaccess file in the relevant directory with the following:

    Code:
    RemoveHandler cgi-script .pl .py .cgi




    Dramatically Speed up your site by implementing Caching!

    Code:
    # MONTH
    <FilesMatch "\.(flv|gif|jpg|jpeg|png|ico|swf)$">
    Header set Cache-Control "max-age=2592000"
    </FilesMatch>
    
    # WEEK
    <FilesMatch "\.(js|css|pdf|txt)$">
    Header set Cache-Control "max-age=604800"
    </FilesMatch>
    
    # DAY
    <FilesMatch "\.(html|htm)$">
    Header set Cache-Control "max-age=43200"
    </FilesMatch>




    Prevent Files image/file hotlinking and bandwidth stealing

    Code:
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?askapache.com/.*$ [NC]
    RewriteRule \.(gif|jpg|swf|flv|png)$ http://www.askapache.com/legal.gif [R=302,L]




    ErrorDocuments

    Code:
    ErrorDocument 404 /favicon.ico
    ErrorDocument 403 https://secure.htaccesselite.com
    Code:
    ErrorDocument 404 /cgi-bin/error.php
    ErrorDocument 400 /cgi-bin/error.php
    ErrorDocument 401 /cgi-bin/error.php
    ErrorDocument 403 /cgi-bin/error.php
    ErrorDocument 405 /cgi-bin/error.php
    ErrorDocument 406 /cgi-bin/error.php
    ErrorDocument 409 /cgi-bin/error.php
    ErrorDocument 413 /cgi-bin/error.php
    ErrorDocument 414 /cgi-bin/error.php
    ErrorDocument 500 /cgi-bin/error.php
    ErrorDocument 501 /cgi-bin/error.php
    Note: You can also do an external link, but don't do an external link to your site or you will cause a loop that will hurt your SEO.
    Last edited by Croc Hunter; 3-16-08 at 06:59 AM. Reason: Please ask a Moderator if you need to update stickies

  2. #2
    target='_blank' snowmaker's Avatar
    Join Date
    Nov 2002
    Location
    Not in Solomons anymore.
    Posts
    3,441
    Rep Power
    21

    .htaccess code examples for an add'l domain

    Here are a few .htaccess code examples gathered from this forum to add to your main .htaccess file that will enable use of another domain name for your package.

    Add the additional domain name in OPS, and follow the additional instructions there.
    http://members.powweb.com/webControl/domReg.bml
    If the domain name is purchased thru Powweb, I believe this step will not be neccessary.

    When the new domain has fully propogated the internet, and accessing the new domain name address yields the same website as the original domain name, continue.

    Create a directory (subfolder) under (within) the htdocs directory and put all the files for the next site there. The .htaccess file should be in the main htdocs directory, not the newly created directory (subfolder).

    Replace domain_name with the next domain name, and tld with the domains' extension (com OR net OR whatever it is).
    Replace subfolder with your subfolders name, and remember that all directory (and file names for that matter) are case sensitive on this system.

    Code:
    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domain_name\.?tld [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.domain_name\.?tld [NC]
    RewriteCond %{REQUEST_URI} !/subfolder/
    RewriteRule ^(.*)$ subfolder/$1 [L]
    Code:
    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /
    RewriteCond s%{HTTPS} ^((s)on|s.*)$ [NC]
    RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]
    RewriteCond %{HTTP_HOST} ^domain_name\.?tld [NC,OR]
    RewriteCond %{HTTP_HOST} ^www.domain_name\.?tld [NC]
    RewriteCond %{REQUEST_URI} !/subfolder/
    RewriteRule ^(.*)$ subfolder/$1 [L]
    Code:
    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /
    RewriteRule ^([^\.]+[^/])$ http://%{HTTP_HOST}/$1/ [R=301,L]
    RewriteCond %{HTTP_HOST} ^(www\.)?domain_name\.tld$
    RewriteCond %{REQUEST_URI} !^/subfolder/
    RewriteRule ^(.*)$ subfolder/$1 [L]
    Code:
    RewriteEngine On 
    Options +FollowSymlinks 
    RewriteBase /
    RewriteCond %{HTTP_HOST} domain_name.tld 
    RewriteCond %{REQUEST_URI} !subfolder/htdocs/
    RewriteRule ^(.*)$ domain_name/htdocs/$1 [L]
    These three lines only need to be used once (at the top of the .htaccess file) if three or more domains are to be redirected.
    Code:
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    To be noted, only one of these code examples is needed, so if one variation doesn't work in your situation, try another..

    With this you can access any number of domains created in htdocs/anydomain.com/htdocs
    Code:
    RewriteCond %{HTTP_HOST} !youruser.powweb.com
    RewriteCond %{REQUEST_URI} !htdocs [NC]
    RewriteRule ^(.*)$ %{HTTP_HOST}/htdocs/$1
    Last edited by snowmaker; 4-8-07 at 09:37 AM.
    -bruce /* somdcomputerguy */
    'If you change the way you look at things, the things you look at change.'

  3. #3

  4. #4
    Mod.. with bite.. Croc Hunter's Avatar
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    7,332
    Rep Power
    26
    I have various setups and a complex .htaccess file with all kinds of code. I wrote these codes and tested, it works for subdomains inside and/or outside of your public htdocs folder.

    You only need this once at the start then repeat any combination of the three lines you require.

    Code:
    RewriteEngine On
    RewriteBase /
    # --------directory structure /subfolder/htdocs/ -----------

    htdocs/.htaccess
    subfolder/htdocs/
    blog/htdocs/

    Code:
    RewriteCond %{HTTP_HOST} subname.yourdomain.com
    RewriteCond %{REQUEST_URI} !subfolder/
    RewriteRule ^(.*)$ subfolder/$1 [L]
    
    RewriteCond %{HTTP_HOST} blog.yourdomain.com
    RewriteCond %{REQUEST_URI} !blog/
    RewriteRule ^(.*)$ blog/$1 [L]
    # --------directory structure /htdocs/subfolder/ -----------

    htdocs/.htaccess
    htdocs/subfolder/
    htdocs/blog/

    Code:
    RewriteCond %{HTTP_HOST} ^(www\.)?subname.yourdomain\.com$
    RewriteCond %{REQUEST_URI} !^/subfolder/
    RewriteRule ^(.*)$ subfolder/$1 [L]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?blog.yourdomain\.com$
    RewriteCond %{REQUEST_URI} !^/blog/
    RewriteRule ^(.*)$ blog/$1 [L]
    The .htaccess goes inside htdocs ie: htdocs/.htaccess
    If you also have a .htaccess outside of htdocs/ these codes may fail.
    Last edited by Croc Hunter; 3-24-08 at 08:53 AM.
    Croc Hunter MSC :

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •