How to redirect to a new URL using .htaccess

  • Home
  • /
  • Blog
  • /
  • How to redirect to a new URL using .htaccess

May 16, 2015

How to redirect to a new URL using .htaccess

By NickLitten

May 16, 2015

htaccess, redirect, URL

“So, I have my name as a domain (www.NickLitten.com) and I want to use as a link to a specific node on the nicklitten.com main site – namely my RESUME page at (www.nicklitten.com/nick.litten)… I wonder how I can redirect a domain name to a specific page on my website?”

Sounds like a simple question but there are a million websites out there in super-web-highway land all waffling on about global redirects, PHP reroutes, meta-ding-dongs and very clever .htaccess files.

The problem is that all of them seem to be a rewrite of the same few paragraphs saying things like this:

Redirect Old domain to New domain (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain. he .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Please REPLACE www.newdomain.com in the above code with your actual domain name. In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website. Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Redirect to www (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} “domain.com [nc]
rewriterule “(.*)$ http://www.domain.com/$1 [r=301,nc]

Please REPLACE domain.com and www.newdomain.com with your actual domain name. Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Now, if you fancy a straightforward route which is easy to maintain going forward then creating and edditing a .HTACCESS file will be the obvious route. Luckily, if you are a Drupaleer then you will find a file called .htaccess in the root of your installation.

I add the these lines to (a) always push any links to the main www.nicklitten.com domain and (b) redirect the nicklittten.com to the correct nicklitten.com node:

RewriteCond %{HTTP_HOST} !”www\. [NC]
RewriteRule ” http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} (.*)\nicklitten\.com
RewriteRule “(.*) /node/700/$1 [R=301,L]
Neat stuff.
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips

>