If you have a site with parts of it using SSL, but want to turn it off for mundane pages like the blog section then use the following code. This uses the $_SERVER['HTTPS'] variable to see if HTTPS is turned on, if it is then a header is issued and the page redirected.
if($_SERVER['HTTPS'] == 'on'){
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header('Location: '.$url, true, 301);
exit();
}
You can turn it back on again on your secure pages using the opposite.
if($_SERVER['HTTPS'] != 'on'){
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header('Location: '.$url, true, 301);
exit();
}
Don’t include both of these scripts on the same page or you will break the site! Also, make sure that no headers have been issued before hand as this code can cause a "headers already issued" error.
To redirect from HTTPS to HTTP on the home page only using the following rule.
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301,L]
The variable %{HTTPS} will be either "on" or "off" and will be enabled even if SSL is not installed on your site. The rule above sees that HTTPS is on and redirects the home page to the HTTP version. You can even chain lots of rules together like this.
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301]
RewriteRule ^inner/directory/?$ http://%{SERVER_NAME}/inner/directory/ [R=301,L]
Note that you should end your last rule with L so that no other rules on the page are run. Also, you need to make absolutely sure that you are not redirecting any pages that are integral to the security of your shopping cart as this will turn off HTTPS for those pages.
You can also do the same thing using the ${SERVER_PORT} variable.
RewriteCond %{SERVER_PORT} 443
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301,L]
The port for HTTPS is 443 so if the port being communicated through is 443 we need to redirect.
There are two main ways in which to redirect the browser using JavaScript, both of which look at the location of the window object. These are the location.href property and location.replace function.
location.href
The following example will cause the page to redirect to another page, keeping the browser history. This might seem like a minor point, but if you redirect a user to another page they will be able to click back, which will mean that they are redirected again.
<script type="text/javascript">
window.location.href="http://www.talkincode.com/";
</script>
location.replace
This works in the same way as location.href, but in this case the redirection will occur and no history will be kept in the browser. This means that if a user visits the page with this code on they will instead see a different page. This means that the user can’t click on the back button and view the page that redirected them. Here is an example of its usage.
<script type="text/javascript">
window.location.replace("http://www.talkincode.com/");
</script>
You can also add this to a link by doing the following, although the user wouldn’t be able to get back to the page that redirected them.
<a href="javascript:window.location.replace('http://www.talkincode.com/')" title="Talk In Code">Talk In Code</a>
It should be noted here that you will probably want to avoid doing this sort of redirect unless you really have to. For example, if you relied on a JavaScript redirect in an important part of a form process and the user has JavaScript turned off then the process is quite likely to break. The ideal situation is to use server side scripts and redirect rules to force the browser to redirect in a more robust manner. Also, in terms of SEO using a JavaScript redirect is pointless as search engine spiders will probably not understand them.
I had a situation the other day where I had an application in Zend Framework and I wanted to redirect a user to another page. This is fine if you are inside a controller as you can use the _redirect() controller function, but in this instance I was in a plugin that didn’t have direct access to the controller.
The solution is to use the getResponse() method, which is accessible to plugins, and which will retrieve the response object. The response object has a function called setRedirect() that is used to redirect. Any headers that have been issued will be overwritten by this function. The following code can be run inside your zend framework plugins to redirect the user to a different page.
$this->getResponse()->setRedirect('http://another/page/', 301);
The setRedirect() function takes two parameters. The first is the URL to be redirected to and the second is the HTTP response code.
To stop access to a directory (and anything in that directory) all you need is a simple RewriteRule.
RewriteEngine on
RewriteBase /
RewriteRule ^exampledirectory/(.*)$ / [R=301,L]
In this example, if this .htaccess file resides in the root directory of the site and you try to access anything within /exampledirectory you will be redirected back to the root folder. To redirect to another folder (like anotherdirectory) on your web server use the following rule.
RewriteEngine on
RewriteBase /
RewriteRule ^exampledirectory/(.*)$ /anotherdirectory [R=301,L]
Recent Comments