mod_rewrite would be preferable to mod_alias for several reasons.
clickfire
8:44 pm on Nov 8, 2011 (gmt 0)
I tested this based on your remarks and seems to work.
RewriteEngine on RewriteCond %{HTTP_HOST} olddomain\.com$ [NC] RewriteRule ^$ [newdomain.com$1...] [R=301,L]
Thanks :)
lucy24
10:27 pm on Nov 8, 2011 (gmt 0)
You forgot
(www\.)?olddomain\.com
and possibly (depending on server's mood, apparently)
^/?$
What's the $1 for? You're only aiming for empty requests-- that is, the index page alone-- so there isn't anything to capture.
clickfire
10:58 pm on Nov 8, 2011 (gmt 0)
I just used the $1 because it was in the example on the page g1smd referenced. Would you be able to list the lines as they should look in your view and let me test?
g1smd
11:00 pm on Nov 8, 2011 (gmt 0)
The $1 is always empty/undefined as the RegEx doesn't capture anything (nor should it).
Root URL should end with a trailing slash.
Use example.com in this forum to stop URL auto-linking.
lucy24
11:11 pm on Nov 8, 2011 (gmt 0)
The www part is for the {HTTP_HOST}. I assumed you're redirecting both www.olddomain.com and olddomain.com. Here the parenthetic (www\.)? means "either with or without the whole www. package".
The ^/?$ replaces your ^? In mod_rewrite, leading slashes aren't supposed to count, but it doesn't hurt to allow for the possibility. Come to think of it, you might even go to
RewriteRule ^/?(index\.html?)?$ et cetera
to grab people who explicitly type "index.html" (or whatever your extension is). This kind of thing apparently depends on your server, and if you're on shared hosting it may be out of your control. I can only say that I've found by direct personal experience that some rules will only work if I put "html" in there.
The ending $ on the condition stops requests with a port number being redirected.
To be sure of the intent you should show .com and .net for the two URLs. Using .com in both would otherwise lead to an infinite redirect loop.
catkin
5:00 am on Nov 11, 2011 (gmt 0)
Is there a good reason to use ^/?$ rather than ^/*$ ? Would the second not also catch [/...] mis-types?
lucy24
5:36 am on Nov 11, 2011 (gmt 0)
Today's lesson: there is absolutely no limit to the Forums' built-in auto-linking capacity ;)
Honestly now. Don't people even look at their own posts after submitting? That's when the most egregious typos jump out at you, especially if you don't Preview.
The form /* will not catch typos at the http:// end, because the domain name is not part of the Rule. It will catch typos in the form
www.example.com//
If you have a lot of visitors who hand-type addresses in spite of mobility impairment-- or a lot of unhelpful partners who consistently misspell links-- then sure, you could use the * form. But generally we don't write htaccess to allow for every possible error. Between the browser's "sorry, can't find it, check your typing" message and the custom 404 page, the user should get the idea anyway.