Showing posts with label htaccess. Show all posts
Showing posts with label htaccess. Show all posts

Monday, 14 March 2016

.htaccess RewriteRule Examples

.htaccess RewriteRule Examples

In Web Development, Many times we need to make Query string parameter as SEO Friendly, For this we need to make a changes in htacess file.
Benefits of SEO Friendly URLs.
  1. URL Readability become better
  2. Better Indexing by Search Engines
  3. Its hard to memorise the query string parameter
  4. Query string paramter in URL looks un-professional


Following are useful mod_rewrite RewriteRule EXAMPLES which you can use in your website.
Example 1
Original URL: http://domain.com/search.php?page=1
Rewritten URL:http://domain.com/search.php/page/1
.htaccess Rule:
RewriteEngine On
RewriteRule ^search.php/page/([0-9]+)?$ /search.php?page=$1 [L]



Example 2(Same as Above AND ".php")
Original URL:http://domain.com/search.php?page=1
Rewritten URL: http://domain.com/search/page/1
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/page/([0-9]+)?$ /search.php?page=$1 [L]



Example 3
Original URL:http://domain.com/search.php?category=vehicles
Rewritten URL:http://domain.com/search/category/vehicles
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/category/([a-zA-Z0-9]+)$ /search.php?category=$1 [L]



Example 4
Original URL: http://domain.com/search.php?category=vehical&page=1
Rewritten URL: http://domain.com/search/category/vehicles/page/1
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/category/([a-zA-Z0-9]+)+/page/([0-9]+)?$ /search.php?category=$1&page=$2 [L]



Example 5
Original URL:http://domain.com/search.php?gender=men&department=clothing&products=tshirts[L]
Rewritten URL: http://domain.com/buy/men/clothing/tshirts
.htaccess RULE:
RewriteEngine On
RewriteRule ^buy/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$  /search.php?gender=$1&department=$2&products=$3[L]



Example 6
Original URL: http://domain.com/search.php?gender=men&department=clothing&products=tshirts&page=1
Rewritten URL:http://domain.com/buy/men/clothing/tshirts/page/2
.htaccess RULE:
RewriteEngine On
RewriteRule ^buy/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/page/([0-9]+)?$ /search.php?gender=$1&department=$2&products=$3&page=$4[L]


Note: You NEED NOT to include "RewriteEngine On" every time, Include once at top of .htacess file


Tuesday, 23 February 2016

301 or 302 Redirection With PHP and benefits

301 or 302 Redirection With PHP and benefits

Question: What is Redirection?
On a Web site, redirection is a technique for moving visitors to a different Web page OR Different Website.


Question: Why we do Redirection?
We do Redirection, because we want to tell the visitor that current page is not appropriate for you.

Following are different reason, we do Redirection
  1. If user already login, and try to open the login page redirect him to user dashboard.
  2. Trying to access a page which is temporary not available.
  3. If user trying to access un-authorized page, Redirect him to user dashboard.
  4. Tring to access non-exist page, redirect him sitemap page OR 404 page.
  5. Trying to access non-exist website.


Question: What are different type of redirects?
  1. 301 permanent redirects
  2. 302 temporary redirects


Question: What is 301 permanent redirects?
301 refers to the HTTP status code.
A 301 redirect is a permanent redirect which passes between ranking power(>90%) to the redirected page.


Question: When we use 301 redirects?
  1. When a web page permanently removed, we redirect user to new page.
  2. When we want to transfer the ranking benefits(SEO point of view) to new web page.
  3. Redirect user to new web page and stopped him to access old page(not available page).
  4. If you have updated the contents to new web page, MUST USE 301 Redirects
  5. 301 indicates to both browsers and search engine bots(Google, bing, yahoo) that the page has moved permanently.



Question: What is 302 temporary redirects?
302 refers to the HTTP status code.
A 302 redirect is a temporary redirect. We have to tell user its temporary unavailable and will be available very soon.


Question: When we use 302 redirects?
  1. If user already login, and try to open the login page redirect him to user dashboard.
  2. Trying to access a page which is temporary not available.
  3. If user trying to access un-authorized page, Redirect him to user dashboard.


Question: How to make 301 redirect with php?
header('Location: http://www.example.com/new-web-page.php', true, 301);exit;



Question: How to make 302 redirect with php?
header('Location: http://www.example.com/new-web-page.php', true, 302);exit;

OR
header('Location: http://www.example.com/new-web-page.php');exit;

Both are same and works same.


Question: Can we use regular express in Redirection?
Yes, See Example:
if(preg_match("/^\/old\./", $_SERVER['REQUEST_URI'], $m)){
    header('Location: http://www.example.com/new-web-page.php', true, 301);exit;
}
URL start with /old will be redirect to http://www.example.com/new-web-page.php


Question: Give an URL to check 301 redirects?
http://www.aboutcity.net/youtube/videos/watch/wEVPkxFC0NI


Question: Where can I test redirect online? http://www.redirect-checker.org/index.php
(Check Your Redirects and Statuscode 301 vs 302, meta refresh & javascript redirects)


Question: How to make 301 redirect with htaccess?
Add following code in htaccess
Redirect 301 /old/ /new/

Make Sure "RewriteEngine On" and installed "mod_rewrite" on server.


Question: How to make 302 redirect with htaccess?
Add following code in htaccess
Redirect 302 /old/ /new/

Make Sure "RewriteEngine On" and installed "mod_rewrite" on server.

Question: What are different HTTP status code used in web?
301 - Permanent movement(redirection)
302 - Temporary movement(redirection)
400 - Bad request
401 - Authorization Required
For More details http://www.web-technology-experts-notes.in/2014/02/htaccess-code-snippets-example.html


Friday, 21 August 2015

How to redirect http to https in Zend Framework

How to redirect http to https in Zend Framework

http to https, Follow the following:
  1. Open htaccess file in /public OR /public_html.
  2. Add following code in your .htaccess at top
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
  3. Save the File .
  4. Refresh the webpage, you will see same page is opened with https.
If you are getting infinite loop, then it means you are using CloudFlare or a similar CDN. Now instead of above code, use below one.
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]



Tuesday, 16 June 2015

Redirect internal links to another file with .htaccess

Redirect internal links to another file with .htaccess


Requirement:
When someone open http://domain.com/projectone.html, User should be able to see the same URL in Browser(i.e. http://domain.com/projectone.html). But Internally, application should call a file http://domain.com/projectone.php. But user should not get to know this.

Solution:
This is 100% achievable by .htaccess in single line.
But your application must allow and support the .htaccess.
RewriteEngine On
RewriteRule ^(projectone\.html)$  projectone.php [R=301,NC,L]


Following are the means of htacess flag used above.
R 301- means permanent redirect.
NC- means this rule is case -insensitive.
L- mens stop processing to next set, if the rule matches, no further rules will be processed.

Tuesday, 5 May 2015

htaccess allow for IP address and should not ask for Authentication

htaccess allow for IP address and should not ask for Authentication

Requirement:

Working on some project in office where all developer have same IP Address. Now many times we want our project must be accessed by offer without authentication.

But some one trying to access the same website from outside of office, It must prompt for password.


In this below code, We are allowing mulitple IP Address for which it will not ask authentication.
Order deny,allow
Deny from all
Allow from "112.xxx.x.xxx|113.xxx.x.xxx|113.xxx.x.xxx"
AuthType Basic
AuthUserFile /opt/.htpasswd
AuthName "Protected Area"
require valid-user
Allow from "112.xxx.x.xxx|113.xxx.x.xxx|113.xxx.x.xxx"
Satisfy Any



Monday, 4 May 2015

How can I make a redirect page using javascript jQuery PHP and htaccess

How can I make a redirect page using javascript jQuery PHP and htaccess

Redirection with JavaScript OR jQuery.

For redirect a page to another page in client side you can use window.location. For this you need not to include the jQuery file.

Question: What is window.location?
The window.location is a object which have all the values like host, href, port and username etc of current location. It is used to get the current page address (URL) and to redirect to a new page.

When I do console.log(JSON.stringify(window.location)), It print following data.
 
{"href":"http://www.web-technology-experts-notes.in/2014/10/jquery-technology.html","origin":"http://www.web-technology-experts-notes.in","protocol":"http:","username":"","password":"","host":"www.web-technology-experts-notes.in","hostname":"www.web-technology-experts-notes.in","port":"","pathname":"/2014/10/jquery-technology.html","search":"","hash":""}

To use the window.location, Its not necessary to include the jQuery.



Question: What is difference between window.location and window.location.href?
window.location is a object which have all the values of current location. I can be used to get the current url and redirect to another website OR page.

window.location.href is one of the property of window.location which is used to get the current url and redirect to another website OR page.


Question: How to redirect a page.
window.location="http://web-technology-experts-notes.in";



Question: How to redirect a page using javascript on onclick event.
 <a href="javascript:void(0);" onclick="redirectPage()">Click to Redirect</a>
 
function redirectPage(){
    window.location="http://web-technology-experts-notes.in";
}


Redirection with PHP

Question: How to redirect to another page.
header("Location: /another-page.php");


Question: How to redirect to another website.
header("Location: http://web-technology-experts-notes.in");


Question: How to redirect to another website temporary.
header("Location: http://web-technology-experts-notes.in", true, 302);


Question: How to redirect to another website permanent.
header("Location: http://web-technology-experts-notes.in", true, 301);


Following are the header codes and their values.
301 - Permanent movement
302 - Temporary movement
400 - Bad request
401 - Authorization Required
403 - Forbidden
404 - Page Not Found
500 - Internal Server Error



Redirection with htaccess

Question: How to redirect to another page directory.
Redirect / /newdir

Question: How to redirect to another page.
Redirect /about.html /pages/about







Thursday, 9 April 2015

Htaccess interview Questions and Answers for fresher and experienced

Htaccess interview Questions and Answers for fresher and experienced



Question: How to redirect http to https?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]


Question: How to redirect http:// to https://www?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]


Question: How to redirect non-www to www ?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


Question: How to redirect all pages to newdomain?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com [R=301,L]


Question: What is meaning of R & L flag in htaccess?
htacces R flag causes a HTTP redirect to be issued to the browser and 301 means its permanent redirect.
htaccess L fag causes to stop processing the rule set, if the rule matches, no further rules will be processed.
Also we can set more than 2 flags in brackets []
More flags


Question: How to redirect homepage to another Website?
Redirect / http://www.web-technology-experts-notes.in/


Question: How to redirect to another directory within same domain?
Redirect /olddir /newdir


Question: How to set environment in .htaccess?
SetEnv APPLICATION_ENV development


Question: How to set the php config variables in .htaccess?
php_value upload_max_filesize 32M


Question: How to set display_error off in .htaccess?
php_flag display_errors off
php_flag display_startup_errors off


Question: How to block few IP address?
order allow,deny
deny from xxx.xxx.xxx.xxx #specify a specific address
allow from all


Question: How to redirect to error/404.html when 404 errors comes?
ErrorDocument 400 error/404.html


Queston: How to set the caching for javascript/images/css?
ExpiresActive On
ExpiresByType application/javascript "now plus 3 day"
ExpiresByType application/x-javascript "now plus 3 day"
ExpiresByType image/jpg "now plus 1 week"
ExpiresByType image/jpeg "now plus 1 week"
ExpiresByType image/png "now plus 1 week"
ExpiresByType image/pjpeg "now plus 1 week"
ExpiresByType image/gif "now plus 1 week"
ExpiresByType text/css "now plus 3 day"


Question: what is meaning of 301, 302, 400, 401, 403, 404 and 500 error codes?
301 - Permanent movement
302 - Temporary movement
400 - Bad request
401 - Authorization Required
403 - Forbidden
404 - Page Not Found
500 - Internal Server Error


Question: How does RewriteBase works in .htaccess?
It is used to set a base URL for your rewrites rules.
RewriteEngine On
RewriteBase /~new/


By setting the RewriteBase there in htaccess, you make the relative path come off the RewriteBase parameter.


Question: How to create Subdomains on the fly using .htaccess?
for this, you have set the virtual host
Follow the following steps.
Step 1: Create a wildcard DNS entry
*.domain.com.   3600  A  127.0.0.1

Step 2: Setup the Virtual Host, in vhost file.

<virtualhost> DocumentRoot "E:\wamp\www\myproject" ServerName myproject.domain.com <directory myproject="" wamp="" www=""> AllowOverride All Order allow,deny Allow from all </directory> </virtualhost>

Step 3: Now write your all codes in in Document Root path i.e.E:\wamp\www\myproject
Step 4: Done, now you can access with myproject.domain.com


Question: Do i need to restart the apache after chnages in .htaccess?
No,


Wednesday, 4 March 2015

How to redirect https to http in htaccess

Today, htaccess play very important role for SEO, It is used for redirection, rewriting and apache config variable etc.
How to edit your htacess file.
Open your .htaccess file which is mostly in root folder OR in public/public_html folder.
Use following code as per your requirement.(Please have backup your .htaccess file before editing).


https://example.com to http://example.com redirect
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


https://example.com to http://www.example.com redirect
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


http://example.com to https://example.com redirect
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]


http://example.com to https://www.example.com redirect
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]


Tuesday, 3 March 2015

non-www Redirect to www htaccess

non-www Redirect to www htaccess

If you want to redirect http://example.com to http://www.example.com
Open your .htaccess file and add following code.

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



Tuesday, 20 January 2015

Wordpress redirect https to http with htaccess [SOLVED]

Wordpress redirect https to http with htaccess [SOLVED]

You can redirect all website pages from https to http. It will be redirect only when you open the page with https. 

See Example Below
https://example.com Redirect to http://example.com
https://example.com/page1/ Redirect to http://example.com/page1/
https://example.com/page2/ Redirect to http://example.com/page2/


Open .htaccess in root folder
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Replace with Following code
RewriteEngine On

RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} on
RewriteRule  ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Wednesday, 10 December 2014

Htaccess RewriteRule Flags by Code Example

Htaccess RewriteRule Flags by Code Example

A RewriteRule can be modified by flag. Flags are included in square brackets at the end of the rule. We can also and multiple flags are separated by commas.

For Example:
[Flag1] //for 1 flag
[Flag1,Flag2] //for 2 flags
[Flag1,Flag2,Flag3 ] //for multiple flags

Following are most commonly htaccess flag:
1. htacces B(escape backreferences) Flag
Escape non-alphanumeric characters before applying the transformation


2. htaccess C(Chain) Flag
First htaccess Rule will be applied to next htaccess rule. Means if the rule matches, then control moves on to the next rule.


3. htaccess CO(Cookie) Flag
you can set a cookie when a particular RewriteRule matches
RewriteRule ^search/(.*)$ /search.php?term=$1[CO=variable:variableValue]
Set the variable=variableValue in cookie


4. htaccess DPI(discardpath) Flag
PATH_INFO portion of the rewritten URI to be discarded.


5. htaccess E (env) Flag
You can set the value of an environment variable. For example:
RewriteRule ^search/(.*)$ /search.php?term=$1[E=variable:variableValue]
Set the variable=variableValue in environment variable.


6. htaccess END Flag
This flag terminates not only the current round of rewrite but also prevents any subsequent rewrite from occurring in per-directory.


7. htaccess F(forbidden) Flag
This flag causes the server to return a 403 Forbidden status code to the client. For Example
RewriteRule \.exe - [F]
If anyone trying to open any file with .exe, he will get Forbidden error.



8. htaccess G(gone) Flag
This flag causes the server to return a 410 Gone status with the response code to the client. For Example
RewriteRule old_url - [G]
If old_url called, 410 gone status will be shown in the browser.



9. htaccess H(handler) Flag
This flag causes Forces the request to be handled with the specified handler. For example
RewriteRule !\. - [H=application/x-httpd-php]
Force all files without a file extension to be parsed by the php handler.



10. htaccess L(Last) Flag
This flag causes to stop processing the rule set, if the rule matches, no further rules will be processed.
RewriteRule ^(.*) /index.php?req=$1 [L]



11. htaccess N(Next) Flag
This flag causes ruleset to start over again from the top.
RewriteRule ^(.*) /index.php?req=$1 [N]


12. htaccess NC(nocase) Flag
This flag causes RewriteRule to be matched in a case-insensitive manner.
RewriteRule ^(.*) /index.php?req=$1 [NC]



13. htaccess NE(noescape) Flag
By default, special characters, such as & and ?, are converted to their hexcode equivalent. By Using NE flag prevents we can prevent this.


14. htaccess NS(nosubreq) Flag
This flag causes prevents the rule from being used on subrequests.



15. htaccess P(proxy) Flag
This flag causes the request to be handled by proxy request. For example, if you wanted all image requests to be handled by a image server
RewriteRule /(.*)\.(jpg|gif|png)$ http://images.mysite.com/$1.$2 [P]
Extension having jpg, gif, and png will be handled by images.mysite.com



16. htaccess PT(passthrough) Flag
This flag causes the result of the RewriteRule to be passed back through URL mapping.



17. htaccess QSA(qsappend) Flag
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

18. htaccess S(Skip) flag
htaccess S flag is used to skip rules that you don't want to run. The syntax of the skip flag is [S=N], where N means the number of rules to skip. For Example
# Does the file exist?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Create an if-then-else construct by skipping 3 lines if we meant to go to the "else" stanza.
RewriteRule .? - [S=3]

# IF the file exists, then:
    RewriteRule (.*\.gif) images.php?$1
    RewriteRule (.*\.html) docs.php?$1
    # Skip past the "else" stanza.
    RewriteRule .? - [S=1]
# ELSE...
    RewriteRule (.*) 404.php?file=$1
# END


19. htaccess T(type) flag
htaccess T flag cause sets the MIME type with which the resulting response will be sent. For Example
RewriteRule IMG - [T=image/jpg]


20. htaccess R(Redirect) flag
htacces R flag causes a HTTP redirect to be issued to the browser. Normally Redirect does not occur but if we use R Flag, then redirection will be happened to the browser. For Example
RewriteRule (.*\.gif) images.php?$1 [R]

Saturday, 22 February 2014

htaccess code snippets example

htaccess code snippets example

.htaccess file is used for configuration on File Level/Directory Level and its supported by all webserver. Today all types of websites use htaccess technology.



Following are Benefits of .htaccess
  • Mange Error Pages for Better SEO
  • Set PHP Config variable
  • Set Environment variable
  • Password protection for File/Directory
  • Allow/Deny visitors by IP Address
  • Detect OS (like Mobile/Laptop/Ios/Android)
  • Redirection pages 
  • Optimize Performance of website
  • Improve Site Security


Following are Few Example of .htaccess

Redirect Home page to Another Website
Redirect / http://php-tutorial-php.blogspot.in/


Redirect Home page to Another another Directory(i.e newdir)
Redirect / /newdir


Redirect about.html to Another another Directory(i.e /pages/about)
Redirect /about.html /pages/about


Redirect old file to New Path
Redirect /oldfile.html /newfile.html


Set PHP Environment
SetEnv APPLICATION_ENV development


Set max_filesize in php
php_value upload_max_filesize 32M


Set off error in PHP
php_flag display_errors off
php_flag display_startup_errors off


Dedect mobile/laptop and redirect to mobile site
RewriteCond %{HTTP_USER_AGENT} android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|ipad|ipod|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-) [NC]
RewriteRule ^$ http://mobile.domain.com/ [R,L]


Block IP Address
order allow,deny
deny from xxx.xxx.xxx.xxx #specify a specific address
deny from xxx.xxx.xxx.xxx/30 #specify a subnet range
deny from xxx.xxx.* #specify an IP address wildcard
allow from all


Allow IP Address
order allow,deny
allow from xxx.xxx.xxx.xxx #specify a specific address
allow from xxx.xxx.xxx.xxx/30 #specify a subnet range
allow from xxx.xxx.* #specify an IP address wildcard
allow from all


Redirect to 400.html, If 400 error comes
ErrorDocument 400 /errorpages/400.html


Redirect to 403.html, If 403 error comes
ErrorDocument 403 /errorpages/403.html


Redirect to 404.html, If 404 error comes
ErrorDocument 404 /errorpages/404.html


Redirect to 500.html, If 500 error comes
ErrorDocument 500 /errorpages/500.html


Disable Directory Listing
Options ExecCGI Includes IncludesNOEXEC SymLinksIfOwnerMatch -Indexes


Enable Directory Listing
 Options All +Indexes


Password Protection
AuthName "Authentication Section"
AuthType Basic
AuthUserFile /home/username/.htpasswds #Here your password will be stored
#htpasswds file format username:password
Require valid-user


Password Protection but Google can Crawl
AuthName "Under Development"
AuthUserFile /home/website/.htpasswd
AuthType basic
Require valid-user
Order deny,allow
Deny from all
Allow from xxx.xxx.xxx.xxx w3.org htmlhelp.com googlebot.com
Satisfy Any


Set Timezone of the Server
SetEnv TZ America/Indianapolis


301 Redirect Old File
Redirect 301 /old/file.html http://php-tutorial-php.blogspot.in/2013/12/curl-example.html


301 Redirect Entire Directory
Redirect 301 /old/ /new/



301 redirect https to http
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


301 redirect https to http://www
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


301 redirect non www to www with htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


Set Caching for javascript/image/css
ExpiresActive On
ExpiresByType application/javascript "now plus 3 day"
ExpiresByType application/x-javascript "now plus 3 day"
ExpiresByType image/jpg "now plus 1 week"
ExpiresByType image/jpeg "now plus 1 week"
ExpiresByType image/png "now plus 1 week"
ExpiresByType image/pjpeg "now plus 1 week"
ExpiresByType image/gif "now plus 1 week"
ExpiresByType text/css "now plus 3 day"


Error Codes - Defination
301 - Permanent movement(redirection)
302 - Temporary movement(redirection)
400 - Bad request
401 - Authorization Required
403 - Forbidden
404 - Page Not Found
500 - Internal Server Error