.Htaccess Interview Questions
.Htaccess File Interview Questions
.Htaccess File Interview Question for Freshers
What is .Htaccess File ?
.htaccess files are configuration files of Apache Server which provide a way to make configuration changes on a per-directory basis.
.Why do we use this and Where?
Using this .htaccess& file, we can control php.ini and httpd.conf file
Why use .htaccess file
- Define custom error pages
- Banning certain IP addresses from accessing your site
- redirecting users to other pages / sites
- password protecting directories
- enabling certain PHP modules
- enabling SSI (server side includes)
How to define Custom Error Page using .htaccess file ?
.htaccess file for Custom Error Pages
ErrorDocument 404 /filenotfound.html
How to ban certain IP addresses from accessing your site using .htaccess ?
Syntax
order allow,deny deny from 255.0.0.0 deny from 192.45.3.2 allow from all
How to ban visitors accessing your site using .htaccess by Referrer ?
Syntax
RewriteEngine on # Options +FollowSymlinks RewriteCond %{HTTP_REFERER} otherdomain\.com [NC] RewriteRule .* - [F]
How to redirect http to https ?
Syntax
RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
How to redirect non-www to www ?
Syntax
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
How to redirect all pages to newdomain ?
Syntax
RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^olddomain\.com$ [NC] RewriteRule ^(.*)$ http://newdomain.com [R=301,L]
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.
How to redirect to another directory within same domain ?
Syntax
Redirect /olddir /newdir
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
How does RewriteBase works in .htaccess ?
RewriteBase is used to set a base URL for your rewrites rules.
Syntax
RewriteEngine On RewriteBase /~new/
Do i need to restart the apache after chnages in .htaccess ?
No, no need to restart your server after change in .htaccess file.
What is htaccess F(forbidden) Flag ?
This flag causes the server to return a 403 Forbidden status code to the client. Se in below code If anyone trying to open any file with .exe, he will get Forbidden error.
Syntax
RewriteRule \.exe - [F]
How to block few IP address ?
Syntax
order allow,deny deny from xxx.xxx.xxx.xxx #specify a specific address allow from all
How to redirect to error/404.html when 404 errors comes?
Syntax
ErrorDocument 400 error/404.html
How to set the php config variables in .htaccess ?
Syntax
php_value upload_max_filesize 32M
htaccess NC(nocase) Flag ?
This flag causes RewriteRule to be matched in a case-insensitive manner.
Syntax
RewriteRule ^(.*) /index.php?req=$1 [NC]