URL redirect/rewrite using the .htaccess file

Q How to redirect http to https

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Q How to redirect http:// to https://www

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Q How to redirect non-www to www

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Q How to redirect all pages to newdomain

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com [R=301,L]

Q What is meaning of L & R flag in htaccess

htaccess L fag causes to stop processing the rule set, if the rule matches, no further rules will be processed
htacces R flag causes a HTTP redirect to be issued to the browser and 301 means its permanent redirect.

Q How to redirect homepage to another Website

Redirect / http://www.khunt.com/

Q How to redirect to another directory within same domain

Redirect /olddir /newdir

Q How to set the php config variables in .htaccess

php_value upload_max_filesize 32M

Q How to set display_error off in .htaccess

php_flag display_errors off
php_flag display_startup_errors off

Q How to block few IP address

order allow,deny
deny from xxx.xxx.xxx.xxx #specify a specific address
allow from all

Q How to redirect to error/404.html when 404 errors comes

ErrorDocument 400 error/404.html

Q 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"

Q 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

Q How does RewriteBase works in .htaccess

RewriteEngine On
RewriteBase /~new/

Q How to create Password Protection with htaccess.

Step 1.This file .htaccess contains the general configure for valid user .
 
AuthType Basic
AuthName "Password Protected Area"
AuthUserFile D:\aditya\htdocs\rnd/.htpasswd
Require valid-user

Step 2.This file .htpasswd contains username and password.

test:dGRkPurkuWmW2

Comments