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

Friday 23 February 2018

PHP Redirect, 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 need redirection?
We do need redirection, because we want to tell the visitor that current page is not appropriate for you or current page have been moved to new location.

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 redirect?
301 code 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 (Permanent)?
header('Location: http://www.example.com/new-web-page.php', true, 301);exit;



Question: How to make 302 redirect with php (Temp)?
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


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


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