Select Page

 

Htaccess Redirection

 

  • Redirect All Pages to to index.html

To redirect all your pages to index.html or index page you can use following script on your .htaccess. This will redirect all pages/url to index.html. To use index.php instead of index.html simply change index.html to index.php.

 

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/index.html$

RewriteRule .*\.(php|html)$ / [L,R=302]

 

  • Redirect all url to another domain 

To redirect all URL to another domain you can use script below

eg. redirect http://domain.com/anything to http://anotherdomain.com

Options +FollowSymLinks

RewriteEngine on

RewriteRule (.*) http://anotherdomain.com/ [R=301,L]

 

  • Redirect all Pages/URL to root/domain

When you need to redirect all your pages to index page you can use script below.

eg. redirect http://domain.com/contact.php , httpd://domain.com/about.php to http://domain.com

RewriteRule ^.+$ / [R=302,NC,L]

  • Redirects everything after the domain name on the url to the exact same copy on the new domain url:
RewriteEngine on 
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

www.example.net/somepage.html?var=foo redirects to www.newdomain.com/somepage.html?var=foo

  • Redirect non-WWW to WWW

When you keep your main URL as www but when you open URL it also show with www as well, then you can force redirect URL to use www from script below.

eg. http://domain.com to http://www.domain.com

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]

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

 

  • Redirect All Pages/URL to Another 
RewriteEngine on

RewriteCond %{HTTP_HOST} ^yourdomain.com[NC,OR]

RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]

RewriteRule ^(.*)$ http://www.yournewdomain.com/$1 [L,R=301,NC]

 

  • Redirect to https://www

You setup SSL on website but the website still opens on non ssl URL (http) instead of https? Use following script  to force redirect to use https://www on your URL.

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]