Thursday, July 17, 2014

htaccess rules

# ---- Make pages render without their extension in the url
Options +MultiViews


404 custom file not found page

ErrorDocument 404 /filenotfound.php


Prevent directory or File listing :

IndexIgnore *

Redirect entire domain to another domain

Redirect 301 / http://www.devaraju.com/

Redrect individual pages

Redirect 301 /old.html http://www.devaraju.com/new.html

Redirect entire domain to respective pages

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

.htaccess ip bases restriction

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^111\.22\.333\.444$
RewriteCond %{REMOTE_ADDR} !^123\.22\.46\.56$
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|svg|swf|css|ico|js)$ [NC]
RewriteRule ^ /maintenance.html

Thursday, July 10, 2014

.htaccess redirection

# Redirect to domain with www.


RewriteEngine on

# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Redirect to domain without www.

RewriteEngine on

# Redirect to domain without www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]


Do not force www/no-www domain

RewriteEngine on

# Redirect to another domain: www.test.org.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?test\.org$ [NC]
RewriteRule .* http://www.test.org%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^(www\.)?test\.org$ [NC]
RewriteRule .* https://www.test.org%{REQUEST_URI} [R=301,L]


Trailing slash for URLs

RewriteEngine on

# Ensure all directory URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]


301 Redirect Directories

#301 Redirect Entire Directory.
RedirectMatch 301 ^/old/(.*)$ /new/$1


# Block users by IP
order allow,deny
deny from 127.0.0.1
deny from 127.0.0.2
allow from all


Change Default Directory Pages

# Change default directory pages.
DirectoryIndex new

Prevent viewing of .htaccess

# Prevent viewing of htaccess file.
<Files .htaccess>
    order allow,deny
    deny from all
</Files>



Change Default Directory Pages

# Change default directory pages.
DirectoryIndex new


Prevent viewing of htaccess file

# Prevent viewing of htaccess file.
<Files .htaccess>
    order allow,deny
    deny from all
</Files>


Prevent Directory Listing

# Prevent directory listings
Options All -Indexes


Compression

# Compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript



Enable Symbolic Links

# Follow symbolic links.
Options +FollowSymLinks