Friday, April 3, 2015

How to redirect https to http in htaccess?

Today, htaccess play very important role for SEO, It is used for redirectionrewriting 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 
?
1
2
3
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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


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


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

Redirect from http to https using htaccess

Some case you want that only some of the pages to redirect to https but not all pages.
In that case the code in above post will not work hence it will redirect all pages to https.
To redirect only some of the pages you need to use below code in your htaccess file.


RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} order.php [OR]
RewriteCond %{REQUEST_URI} payment.php [OR]
RewriteCond %{REQUEST_URI} success.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
From above code in your website only order.php, payment.php and success.php will be redirect to https protocol.
Here I have used RewriteCond with OR condition. If we use OR condition than Rule will be apply if any of the condition gets true. For more detail about OR condition

What is htaccess? Why do we use this and Where?

.htaccess files are configuration files of Apache Server which provide
a way to make configuration changes on a per-directory basis. A file, 
containing one or more configuration directives, is placed in a particular
document directory, and the directives apply to that directory, and all 
subdirectories thereof.

What are the differences between require and include, include_once and require_once?

The include() statement includes and evaluates the specified file.The documentation below also applies to require()
The two constructs are identical in every way except how they handlefailure.include() produces a Warning while require() results in a Fatal Error. In other words, use require()if you want a missingfile to halt processing of the page.
include() does not behave this way, the script will continue regardless.

The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only differencebeing that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

include_once() should be used in cases where the same file might be included and evaluated more than once during a particularexecution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.

require_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.

Monday, March 16, 2015

How to create a calculator using PHP?

<html>
<head>
<title>Simple PHP Calculator</title>
</head>
<body>
<form method="post" action="calculator.php">
<input type="text" name="value1">
<input type="text" name="value2"><select name="action">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type="submit" name="submit" value="Calculate Now"></form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$action = $_POST['action'];
if($action=="+"){
echo "<b>Your Answer is:</b><br>";
echo $value1+$value2;
}
if($action=="-"){
echo "<b>Your Answer is:</b><br>";
echo $value1-$value2;
}
if($action=="*"){
echo "<b>Your Answer is:</b><br>";
echo $value1*$value2;
}
if($action=="/"){
echo "<b>Your Answer is:</b><br>";
echo $value1/$value2;
}
}
?>
</body>
</html>

Saturday, March 14, 2015

How to Redirect your 404 page to the Home Page in WordPress.


404’s are part of every website. A while ago we compiled a list of some pretty cool WordPress 404 designs. When coding a one page site, you might not have the time to create a custom 404 page. In which case, you might as well redirect the 404 page to your site’s homepage. In this article, we will show you how to redirect 404 page to home page in WordPress.
All you have to do is open your 404.php file in your theme’s folder. If it doesn’t exist, then create a blank php file. Paste the following code in there:
<?php

header("HTTP/1.1 301 Moved Permanently");

header("Location: ".get_bloginfo('url'));

exit();

?>

That’s all. Now when a user hits a 404 page will be redirected to the homepage.
Note: This should be used in very specific cases. In most other cases (i.e blogs, portfolios etc), you should track your 404 pages and redirect them appropriately.

Friday, March 13, 2015

str_repeat function.

Definition

string str_repeat ( string $input , int $multiplier )

Description

Returns input repeated multiplier times.

Parameters




Result

$result (plain text):

Code

<?php
$input 
'*';

$multiplier 5;
$result str_repeat ($input$multiplier);
?>

HOW TO CREATE THE MYSQL BACKUP?

Easy way to create the mysql backup file

<?php 
/* Create the mysql backup file */
define(‘DB_NAME’‘databaseName’);
/** MySQL database username */
define(‘DB_USER’‘dataUser’);
/** MySQL database password */
define(‘DB_PASSWORD’‘dbPassword’);
/** MySQL hostname */
define(‘DB_HOST’‘localhost’);
$dbhost = DB_HOST;
$dbuser = DB_USER;
$dbpass = DB_PASSWORD;
$dbname = DB_NAME;
$sqlfile = $dbname date("Y-m-d") . ‘.sql.gz’;
system("mysqldump -h $dbhost -u $dbuser –password=$dbpass $dbname | gzip > $sqlfile");
?>