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 ^(.*)$ [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 /

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 #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
*   3600  A

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

<virtualhost> DocumentRoot "E:\wamp\www\myproject" ServerName <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

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