20 Useful .htaccess Code Tricks & Tips

Tuesday, February 28, 2017

It is very important that all web admins understand and know how to successfully manage the .htaccess file. This file is mainly responsible for steering your website’s directories, but its functionalities can be extended by adding various lines of code.

We created an excellent collection of 20 useful .htaccess code snippets. These will improve your website and ease your work. Take a look at all of these .htaccess code snippets and see which one you can add to your website.

You can find .htcaccess code snippets about various topics such as iPad detection, error pages, Favicon requests, protecting folders with a password, PHP error logging, 301 redirects, image hotlinking, set expires, append/prepend files, and more.

These code snippets will save a lot of time and effort.  Check them out and don’t forget to backup your .htaccess file before trying any of these.

Custom Error Pages

Check out this great code snippet that you can use to create custom error pages. Just add the code and you will instantly get the results that you want.

ErrorDocument 400 /400.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 405 /405.html
ErrorDocument 408 /408.html
ErrorDocument 414 /414.html
ErrorDocument 500 /500.html
ErrorDocument 502 /502.html
ErrorDocument 504 /504.html

Force Favicon Requests to Correct Location

You can use this code snippet for those times when you need to request a web server for a specific favicon. This snippet saves you a lot of time and effort. Try it out!

# REDIRECT FAVICON.ICO
<ifmodule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://ift.tt/2mBqecC [R=301,L]
</ifmodule>

Force Files to Download (Not Open in Browser)

Here is a very useful code snippet for those times when you want to force the files that you download not to open in your browser. Take a look!

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

iPad Detection

Check out this great code snippet that you can use to detect a specific version for iPad. This code lets you easily identify exactly what you are looking for.

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
RewriteRule ^(.*)$ http://ift.tt/2m49TPX [R=301]

Password Protect Folder(s)

Use this great code snippet to protect your folders with any password that you find fitting. You can also use this kind of protection for all the subfolders.

AuthType Basic
AuthName "This Area is Password Protected"
AuthUserFile /full/path/to/.htpasswd
Require valid-user

PHP Error Logging

Here is a very useful code snippet that logs errors to any file that you choose. Take a look and see if you can use it for your website. All you need to do is to copy – paste this snippet.

# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log /location/to/php_error.log

Temporary Maintenance using Mod_Rewrite

You can use this code lines to rapidly take down a website for maintenance purposes. You will be the only one to see the content of the website, and others will be redirected.

# Don't forget to turn on the rewrite engine
RewriteEngine on

# Maintenance Redirection
# Replace 555\.555\.555\.555 with your own IP address
# Uncomment first conditional to turn off the redirection
# RewriteCond %{REQUEST_URI} ^$a
RewriteCond %{REQUEST_URI} !maintenance.html
RewriteCond %{REQUEST_FILENAME} !(styles|images).+$
RewriteCond %{REMOTE_ADDR} !^555\.555\.555\.555$
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
RewriteRule (.*) /maintenance.html [R,L]

Prevent Image Hotlinking

Check out this great code snippet that l=shows you how to successfully prevent image hotlinking. This makes possible that any image that has a link from any place else than your own website to automatically redirect to a custom graphic that you choose.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yourdomain\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpg|gif|bmp|png)$ /images/dontsteal.jpg [L]

301 Redirects

This is an excellent code snippet that helps you effortlessly redirect an URL. This is SEO friendly and very easy to use. Take a look and use it for your website.

Redirect 301 /oldpage.html http://ift.tt/1DVD2fA
Redirect 301 /oldpage2.html http://ift.tt/1oLxJJF

Append / Prepend Files

You can use this great code snippet to append/prepend files. Take a look at what it actually does by adding it to your website. Try it out for yourself!

php_value auto_prepend_file "/real/path/to/file/functions.php"
php_value auto_append_file "/real/path/to/file/footer.php"

Allow Single URL Through .htaccess Password Protection

Here is another useful code snippet that can be used for staging, production, etc. This lets you maintain your .htaccess files in sync at the same time having a .htpasswd on your development environment.

#allows a single uri through the .htaccess password protection
SetEnvIf Request_URI "/testing_uri$" test_uri

#allows everything if its on a certain host
SetEnvIf HOST "^testing.yoursite.com" testing_url
SetEnvIf HOST "^yoursite.com" live_url
Order Deny,Allow

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user

Different Directory Index Page

Here is a simple code line that does a big job. This lets you instantly change your directory index page very fast and easy. Take a look and try it for yourself!

DirectoryIndex index2.html

Force Correct content-type for XHTML Documents

If your server doesn’t serve the XHTML content as text or as HTML, you can use this code lines to rectify it. All you need to do is to copy-paste this coded and your problem is solved.

RewriteEngine On
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - "[T=application/xhtml+xml; charset=ISO-8859-1]"

Serve SVG with the Correct Content Type

Here is a very useful code line that helps you to serve SVG with the correct content type. Take a look and see exactly what it can do for your website.

AddType image/svg+xml .svg .svgz

Set Expires

Here is a great code snippet that allows your browser to use the cache for specific operations. Check it out and see how it can ease your work.

# BEGIN EXPIRES
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 10 days"
 ExpiresByType text/css "access plus 1 week"
 ExpiresByType text/plain "access plus 1 month"
 ExpiresByType image/gif "access plus 1 month"
 ExpiresByType image/png "access plus 1 month"
 ExpiresByType image/jpeg "access plus 1 month"
 ExpiresByType application/x-javascript "access plus 1 month"
 ExpiresByType application/javascript "access plus 1 week"
 ExpiresByType application/x-icon "access plus 1 year"
</IfModule>
# END EXPIRES

Subdirectories URL Internally Redirect to Query String

This is a useful code snippet that redirects to Query string all the URL subdirectories. Try it out for your website and see what it can do for you!

RewriteEngine on
RewriteRule ^index/([^/]+)/([^/]+).php /page.php?search=$1&sort=$2 [NC]

 Use PHP inside JavaScript

This neat code snippet allows your browser to interpret a file as JavaScript. Check out the full code lines and add them to your website.

<FilesMatch "^.*?api.*?$">
SetHandler php5-script
</FilesMatch>

Fancy Indexing

Use this next code snippet in your websites to add specific features such as fonts, size, date, etc. Take a look at the example and see if it suits your website.

IndexOptions FancyIndexing
IndexOptions FoldersFirst
IndexOptions NameWidth=*

Denying and Allowing Access

This is a great code snippet that you can use to deny or allow access to specific IPs. Check it out and try it on your website for this effect. Enjoy!

Order deny,allow
Deny from All
Allow from xxx.xxx.xxx.xxx
Allow from xxx.xxx.xxx.xxy

Active Gzip Compression

Use compression to diminish the response of the HTTP by reducing the size. Gzip allows you to successfully reduce the size by almost 70%.

# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP

The post 20 Useful .htaccess Code Tricks & Tips appeared first on Web Design Blog | Magazine for Designers.



via http://ift.tt/2m49WLF

No comments:

Post a Comment

 

The Cash Box Blueprint

Most Reading