Jump to content

Recommended Posts

So i thought another good doucmented and updated htaccess thread would be nice, so here it goes:

Warning! make shure to only add the RewriteEngine On once! It may break some things!

Show php errors (displays php errors in browser):

php_flag display_errors on

Enforcing https (will redirect every visit over http to the https version of your site):

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Remove file endings (adds the ability to also access over /file instead of /file.php or /file.html)

remove .php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

remove .html:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Defining custom error pages (enables you to change the defult 404/500/ etc. page):

ErrorDocument 400 /error/bad-request.html
ErrorDocument 401 /error/unauthorised.html
ErrorDocument 403 /error/forbidden.html
ErrorDocument 404 /error/file-not-found.html
ErrorDocument 500 /error/internal-server-error.html

The number after ErrorDocument is the error code number, the path after it the path to the html/php document, while / is your htdocs folder.

This way is preferred over the cPanel way, as it wont give Mixed Content warnings over SSL.

Blocking IPs (will block the ips from visiting):

order allow,deny
deny from 1.2.3.4
allow from all

you can add as many deny from x.x.x.x as you like:

order allow,deny
deny from 1.2.3.4
deny from 81.214.78.92
deny from 9.4.74.8
allow from all

you can also block full ip ranges:

order allow,deny
deny from 85.214.
allow from all

I know this dont makes that much sence as theres an IP blogger in cPanel, but just for reference its here too.

Block your site from getting iframed (will show errors if a site tries to iframe your site):

Header set X-Frame-Options: "DENY"

Block visitors reffered from a defined domain:

RewriteEngine on
RewriteCond %{HTTP_REFERER} example.com [NC,OR]
RewriteRule .* - [F]

you can add as many domains as you like:

RewriteEngine on
RewriteCond %{HTTP_REFERER} example1.com [NC,OR]
RewriteCond %{HTTP_REFERER} example2.com [NC,OR]
RewriteCond %{HTTP_REFERER} example3.com [NC,OR]
RewriteCond %{HTTP_REFERER} example4.com [NC,OR]
RewriteRule .* - [F]

Block user agents (will block defined user agents)

RewriteEngine On  
RewriteBase /  
SetEnvIfNoCase Referer "^$" bad_user
SetEnvIfNoCase User-Agent "^Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1" bad_user
Deny from env=bad_user

You can add as many as you like:

RewriteEngine On  
RewriteBase /  
SetEnvIfNoCase Referer "^$" bad_user
SetEnvIfNoCase User-Agent "^Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1" bad_user
SetEnvIfNoCase User-Agent "^Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36" bad_user
SetEnvIfNoCase User-Agent "^Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36" bad_user
Deny from env=bad_user

Used to ban users using chrome bots, allthough that dont makes that much sence as ifastnet has a antibot system.

Change index page:

DirectoryIndex newindex.html

 

So thats all i have for you for now, hope this helped!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...