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

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]