homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

mod_rewrite assistance please
Changing site names

 5:32 am on Oct 30, 2002 (gmt 0)


I've been going over the extensive information on WebMasterWorld regarding mod_rewrite, but I'm still puzzled as to the best way to go about the following.

The scenario is this.

My client has decided that they want to change the name of the site, so a new domain name will be chosen. This php based site contains dynamic URL's generated through a custom CMS + a phpBB forum. It's a fairly large site, so setting up 301 redirection on a URL by URL basis isn't an option. Everything else will remain the same in relation to file base structure, IP etc. etc. etc., only the domain name will be changed.

What would be the best way to go about this so that current search engine listings are not made invalid, visitors wind up on the right page and SE spiders won't balk at the new domain name. Is mod_rewrite the best way to go and what would be the syntax?

I greatly appreciate your feedback and apologise if this info is already available as plain as day on WebmasterWorld - the more I read, the more I confused myself :0).



 6:20 am on Oct 30, 2002 (gmt 0)


Point the new domain to the existing server with a new DNS entry. Then add the following lines to .htaccess in the root directory of the Web site:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.newdomain\.com$
RewriteRule ^(.*)$ [newdomain.com...] [R=permanent,L]

This reads "If the hostname in the requested URL is not [newdomain.com,...] rewrite the URL using [newdomain.com...] as the hostname, attach the requested URI, and return the resulting new URL, along with a 301-Moved Permanently server response code to the requestor. This is the Last RewriteRule to be processed for this request."

A browser will then re-request the desired page using this updated URL, as supplied by your server.

The above will rewrite all requests for [oldomain.com...] to [newdomain.com....] It will also rewrite [oldmain.com...] and [newdomain.com...] to [newdomain.com....] You can change the behaviour with respect to "www" as desired, by including or excluding the "www\." in the RewriteCond and the "www." in the RewriteRule.

The above can also be used in the httpd.conf configuration file with minor adjustments. Query strings will be preserved. More info [httpd.apache.org].

That's about it - it's not very difficult to do, but I suggest you test it on a production server or at least during the time of least site traffic!

Search engines will see the 301-Moved Permanently response returned by the server, and update their database to show the new URL in their search results. Anyone using a browser to access the site will see the URL in their address bar update to the new URL as well. You will need to "manually" ask that your listings in ODP, Yahoo, and other human-edited directories be updated.

<edit>Fixed typos</edit>

[edited by: jdMorgan at 7:40 am (utc) on Oct. 30, 2002]


 6:53 am on Oct 30, 2002 (gmt 0)


Thanks for going into such detail, and especially the translation of the syntax. Greatly appreciated.


 7:14 am on Oct 30, 2002 (gmt 0)

No worries! :)



 7:44 am on Oct 30, 2002 (gmt 0)


For the life of me, I cannot get one particular line of my post above to read correctly. Note that a space is required between "%{HTTP_HOST}" and "!^www\.newdomain\.com$"

Something in the forum text-formatting code keeps dropping the space.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved