.htaccess Page Redirect From Old Domain to New Domain with query string

Apache .htaccess how to tips and tricks for redirecting from olddomain.com to newdomain.com with and without query string parameters.

If you got new or nicer domain or you want to balance server load if you distribute api’s and want to redirect or move all the pages (url) from olddomian to new domain with or without query string. For example if you have, let say 100 URL’s like

olddomain.com/page-one/ or
olddomain.com/page-two.html or
olddomian.com/page-three.php or
olddomian.com/product.php?id=12 or
olddomian.com/product.php?id=12&color=red or
Any type of url.

Here is how you can set up command on .htaccess file to redirect your old domain with exact url paramaters. That is redirect to the exact page you want by rewriting the domain but keeping the remainder of the path including the url query string intact.

Options +FollowSymLinks 
RewriteEngine On
RewriteRule ^(.*) http://nepali-calendar.com%{REQUEST_URI} [R=301,NC]

Options +FollowSymLinks’ and ‘RewriteEngine On’ must be there when rewriting urls.
The rewriterule will take all requests and point to your new domain and keep whatever directories, file name and query string is in your old links.

  • R=302 is the HTTP code ‘TEMPORARY MOVED’ or you can do R=301 which is permanent moved(redirect)
  • NC – means case insensitive
  • Here http://nepali-calendar.com is the newdomain.com

If you want to redirect everything after sub directory like www.olddomain.com/time/ to newdomain.com with parameter and query intact here is the example.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/subdirname/(.*)$
RewriteRule ^(.*) http://nepali-calendar.com/%1 [R=302,NC]

If you wan to redirect everything after filename like www.olddomian.com/time.php to newdomain.com with parameter and query intact here is the example.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/time.php(.*)$
RewriteRule ^(.*) http://nepali-calendar.com/%1 [R=302,NC]

If you wan to redirect everything like olddomain.com/time.php or olddomain.com/subdirname or just olddomain.com to newdomain.com/time/ with parameter and quary intact here is the example.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/time.php(.*)$
RewriteRule ^(.*) http://nepali-calendar.com/time/%1 [R=302,NC]

The condition that will detect your old domain sub-directory name or filename and extract everything after it and then append that part to the path of your new domain.

The RewriteCond will return everything after the subdirname or filename in the url as the %1 variable. You then place this to the end of your new domain as (http://nepali-calendar.com/%1). Now it will seamlessly redirect whenever it encounter old url which you setup for redirect to new domain with parameters.

Thats it. Its easy but still its dedicate to do with live and stablished website.

Before you processed I suggest to go through some .htaccess tutorial here.

Here are the experiment I have done for above redirect with parameters from www.ashesh.com.np/linknepali-time.php to nepali-calendar.com/time/

Experiment

https://www.ashesh.com.np/linknepali-time.php redirect to http://nepali-calendar.com/time/linknepali-time.php

https://www.ashesh.com.np/linknepali-time.php?time_only=no&font_color=FFFFFF&aj_time=yes&font_size=12&line_brake=0&bikram_sambat=0&nc=1&api=2ac8y373 redirect to http://nepali-calendar.com/time/linknepali-time.php?time_only=no&font_color=FFFFFF&aj_time=yes&font_size=12&line_brake=0&bikram_sambat=0&nc=1&api=2ac8y373

Here is the conclusion from experiment: .htaccess redirect is much faster and consume much less CPU power of your host than php redirect.

  • 9/10
    Please scale form 1-10 for how much it helps you!!! - 9/10
9/10

Please leave your comment below for help and query.

Ashesh

सयौ थुङ्गा फूलका हामी एउटै माला नेपाली, Welcome to my webpage. I'm from the Himalayan Country of Nepal. Well talking about me, I like mostly Web programming and Designing and furthermore I like Philosophical literature, Photography, Social networking. And I am Romantic and Sentimental person to some extent. Read more...