Forum Moderators: phranque

Message Too Old, No Replies

Redirect Domain URL Only

         

clickfire

3:39 pm on Nov 4, 2011 (gmt 0)

10+ Year Member



Is there a way to redirect this

http://www.olddomain.com/


to

http://www.newdomain.com/


without sending other URLs like to

http://www.olddomain.com/blah


to

http://www.newdomain.com/blah


as happens when I try this

Redirect 302 / http://www.domainname.com/

phranque

5:22 am on Nov 5, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



use the RedirectMatch directive instead and be specific with the pattern match:

http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch

g1smd

7:42 am on Nov 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



See: [webmasterworld.com...]

phranque

1:50 pm on Nov 5, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



mod_rewrite would be preferable to mod_alias for several reasons.

clickfire

8:44 pm on Nov 8, 2011 (gmt 0)

10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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)

10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

clickfire

7:48 pm on Nov 10, 2011 (gmt 0)

10+ Year Member



Thanks, this seems to work fine.

# redirect domain url only
RewriteCond %{HTTP_HOST} example\.com$ [NC]
RewriteCond %{HTTP_HOST} (www\.)?example\.com$ [NC]
RewriteRule ^/?$ http://www.example.com$1 [R=301,L]

g1smd

8:41 pm on Nov 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Completely remove the first condition and the $1 from the target URL of the rule.

clickfire

9:09 pm on Nov 10, 2011 (gmt 0)

10+ Year Member



Thanks for catching that. Here's the finished product for posterity's sake.

# redirect domain url only
RewriteCond %{HTTP_HOST} (www\.)?example\.com$ [NC]
RewriteRule ^/?$ http://www.example.com [R=301,L]

g1smd

9:13 pm on Nov 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Target URL should end in trailing slash.

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)

10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.