homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Yes I know bare with me: which 301 redirect is better and why please?

 10:59 am on Feb 6, 2013 (gmt 0)

I'm moving my entire site over to a new domain. All the files should match up 1:1. AKA olddomain/file.php and newdomain/file.php.

In some threads and on this site I see you say use...

Redirect 301 / http://www.example.com/


RedirectMatch 301 ^.*$ http://www.example.com/index.htm

On other sites they say use...

RewriteCond %{HTTP_HOST} !^www\.new_domain\.com
RewriteCond %{HTTP_HOST} !^$
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

Furthermore I ask about the simple one liner above and an experienced forum member said "you would likely introduce an infinite loop using that".

I trust you guys more. So please be so kind and tell which is better? As I want to get this right. Thank you :)



 11:11 am on Feb 6, 2013 (gmt 0)

It depends if the new domain is using the same filespace on the same server, or is on a different server.

The one line fix can introduces several problems especially if both domains point to the same physical hosting space. The infinite redirect loop is but one of them.

Whatever you do, you usually need several extra lines just so you do not introduce multiple step redirect chains for index requests.

# Redirect index.html and .htm to folder on www of new hostname
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]

# Redirect non-canonical to www on new hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Replace the part "
html?" with "php" to suit.

The pattern part "
index\.html?\ HTTP/" will need to be altered if the site uses query string parameters on the end of "index.html" or "index.php" URLs.

The above code goes on the old domain and on the new domain. If both are hosted in the exact same physical filespace, you post it just the once and it still works!

[edit: fix typos]

[edited by: g1smd at 11:29 am (utc) on Feb 6, 2013]


 11:17 am on Feb 6, 2013 (gmt 0)

Hmmm. Unfortunately I dont know what you mean (EDIT: with your first post). I'll tell you what I have and you tell me what I got. K? AKA tell me if it's sharing the same file space?

My host is Hostgator. (EDIT: It's redirecting to the same server. AKA I am not moving hosts only domains.) I have a main domain. And I have many addon domains. These are not sub domains. But full fledged domains. I am needing to redirect one of the addon domains. The addon domains live on the server at /public_html/addondomain

Thank you very much. I know this question annoys you guys so thanks for helping me work it out.


 11:26 am on Feb 6, 2013 (gmt 0)

Ahh yes and I have the the non-cononical stuff in my htaccess too at the old domain and new. Dont know if that makes a difference.

# 301 permanent redirect index.php to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


 11:32 am on Feb 6, 2013 (gmt 0)

Well, the canonical hostname is the new hostname, so in the old hostname htaccess file, tell it that the canonical hostname is the new hostname.

Requests for the old hostname end up at the old htaccess file and instead of redirecting to www of old hostname it will instead redirect to www of new hostname.

So in all three places where "example.com" is mentioned in the htaccess file for the old hostname, you put the NEW hostname.

Do everything with RewriteRule. Forget that Redirect and RedirectMatch ever existed. They are inferior in very many ways.

[edited by: g1smd at 11:42 am (utc) on Feb 6, 2013]


 11:34 am on Feb 6, 2013 (gmt 0)

OH WAIT! My bad. I miss spoke. I just realized. This is probably going to make it easy. They are not going to match from old to new - not 1:1. They are going to be olddomain.file.php and newdoamin/folder/file.php. And the folder changes depending on the file group. So in that case what would be the best way? I have 800+ pages. So it's probably impracticable to redirect each seperatly. So should I just redirect the entire old domain to the new domain root?


 11:37 am on Feb 6, 2013 (gmt 0)

Never redirect multiple sub-pages to the domain root. It's a "signal of low technical quality".

If by "file group" you mean "folder", then you'll need to have a ruleset pair for each folder followed by the code above to catch the remainder that do map 1:1.

If that results in several dozen rules or less then I would do it that way.

There's an alternative way if you have to do the 800 individually:
1. Make a PHP script that has an array with the URL pairs in: "old url" => "new URL". This data will NOT include hostname, just path.
2. Use the PHP HEADER directive to send a 301 redirect out with the new URL.
3. In htaccess on the old hostname, rewrite (that's internally rewrite, not externally redirect) requests for the old hostname to this PHP script.
RewriteCond %{REQUEST_URI} !^/fix-urls\.php
RewriteRule .* /fix-urls.php [L]

4. Ensure that the PHP script returns 404 for requested URLs that are not fulfilled from the data in the PHP array.
5. Make sure the PHP script properly handles named index requests and bare folder requests, and especially those for ROOT.

[edited by: g1smd at 11:52 am (utc) on Feb 6, 2013]


 11:46 am on Feb 6, 2013 (gmt 0)

If it's doable I guess I have no prob. Yeah so there will be about 17 new folders between the file name and the root. Where there were none in the old domain.


 11:48 am on Feb 6, 2013 (gmt 0)

The "difficulty" is not so much there's 17 new folders, but more in "how you identify the new groupings from looking at the old URLs".


 12:03 pm on Feb 6, 2013 (gmt 0)

Ya know, I don't mind so much in this case showing low technical ability. Come to think of it, landing on the new home page is not such a bad thing. Can you show me how to just do a simple redirect everything from old domain to newdomain root. Aka /


 2:21 pm on Feb 6, 2013 (gmt 0)

I don't mind so much in this case showing low technical ability

Yes, you do mind. It is perfectly OK to walk in here and ask elementary questions and admit that you don't know anything. But never ever let the search engines suspect that your site suffers from Low Technical Quality. If some pages on the old site simply have no counterpart on the new site, it is perfectly OK to serve up a 410. Make a very nice custom 410 page explaining that you've been remodeling and some stuff isn't where it used to be.

When g1 talks about "same server" he means, at most, "same userspace". As long as each domain has its own htaccess, it doesn't matter if they live on adjoining sectors of a single disk, or on opposite ends of the continent ;)

If both domains are in the same userspace on shared hosting, you may want to make an extra htaccess in this shared space. (If your host's configuration lets you do this.) Reserve this file for general access control-- your basic Deny from... directives. Any IP that was unwelcome at the old domain will remain unwelcome in the new one, so they should get the same door slammed in their faces either way.


 11:33 pm on Feb 6, 2013 (gmt 0)

:: returning after a full, er, day's sleep ::

Redirect 301 / http://www.example.com/


RedirectMatch 301 ^.*$ http://www.example.com/index.htm

As noted above, you will not use either of these formulations, because combining mod_rewrite (RewriteRule) and mod_alias (Redirect by that name) in a single htaccess can be lethal. You may be able to use both in a config file, because config file = it's your own server, so you know for rock certain which module will execute first. In htaccess you can't make assumptions-- and you can't change the order of modules. You can only order your commands within modules.

I wanted to make sure you understand that the two quoted lines do entirely different things.

The first form grabs the first single / in a request-- whether it's "www.example.com/" alone or "www.example.com/directory/otherdirectory/filename.html" and replaces it with http://www.example.com/ So it will point you to either the same place or a different place, depending on what came before that first / This in turn depends on how many different requests converge on the same htaccess.

The second form grabs all requests-- not just pages but all supporting files-- and replaces their entire content with the specific URL www.example.com/index.htm. In this second form, using RedirectMatch instead of Redirect alone, the ^ and $ anchors are permitted. But this specific rule doesn't require them and would behave exactly the same if you left them out.


 6:40 pm on Feb 7, 2013 (gmt 0)

Ok. I think. So how do I do it then if all my paths are different on my new domain. The old domain was olddomain/file.php and the new domain is newdomain/17-dif-folders/file.php? Thanks!


 8:04 pm on Feb 7, 2013 (gmt 0)

Use multiple RewriteRules each configured as a redirect, or use a single RewriteRule configured to rewrite to a PHP script that then does all the hard work.

Do not use Redirect or RedirectMatch at all.


 12:34 pm on Feb 8, 2013 (gmt 0)

Why not use Redirect or RedirectMatch at all?

You guys seem to care so much that I dont redirect all pages to my root but you dont show me how. Unless I'm shown how I have no choice but to use some one liner found on google


 8:11 pm on Feb 8, 2013 (gmt 0)

Why not use Redirect or RedirectMatch at all?

Because you're on shared hosting.

mod_alias (Redirect by that name) is a perfectly good way to do a simple redirect-- the kind that's done with a conditionless RewriteRule--

except that when it is not your own server, you have no control over the execution order of the modules. It is essential that all redirects (lower case) take place before all rewrites (lower case). If you don't know whether mod_alias executes before or after mod_rewrite, the only way to enforce the correct order is to do everything in the same module.

You said at the beginning
All the files should match up 1:1.

Now, if you are entirely shutting down the old domain, then in fact it is OK to use mod_alias, because the redirect/rewrite part of the old domain's htaccess will be stripped down to two lines. (You may want to keep the access-control parts-- the Deny from... directives-- so you don't waste time redirecting people you're about to lock out.)

If the new domain is in the same userspace as the old domain, these two rules MUST go in the old domain's htaccess, not in anything shared by both domains.

First line:
RedirectMatch 301 ^(([^./]+/)*)index\.html$ http://www.example.com/$1

Second line:
RedirectMatch 301 ^(.*)$ http://www.example.com/$1

In the old domain's htaccess you do not need to look at hosts, requests or anything else, because everything is getting shipped straight over to the new domain.

Do not cut and paste. I haven't used mod_alias in a while so I may have forgotten some crucial detail of the syntax.

Q: Did I just flatly contradict something I said earlier in this very thread?
A: Uhm, yes, I did. But it ONLY applies when you are completely shutting down an old domain and moving to a new one, AND the quoted rules are in a place that is only visible to the old domain.

If, on the other hand, your two domains are in the same userspace and there are no other domains, then there is an even simpler method. For this one you revert to mod_rewrite. But let's pause for now ;)


 5:53 pm on Feb 9, 2013 (gmt 0)

Thanks Lucy for the info. So I tried the strait redirect and it s didn't work in pages other than the root. I know I originally said 1:1 but that's not the case actually. The folder structure and file names are all different in the new domain. So... which snippet should I use?

#Redirect from old domain to new domain
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

#Redirect to www location
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

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

If the answer is non of them please show me what then. Thank you


 10:25 pm on Feb 9, 2013 (gmt 0)

Also. Lets say I didn't want to be lazy. What's the best way then to redirect about a 1000 pages all with different paths in the new domain? And no there is no common amoung them. So I would have to redirect each one. So just same redirect code x a 1000 then?


 11:44 pm on Feb 9, 2013 (gmt 0)

Yup. You can put 1000 lines in htaccess, where each one is a simple conditionless RewriteRule

:: pause to count on fingers ::

Say 100 chars/line x 1000 lines = 100 000 bytes = about 100K

(Let us not speculate about why I originally arrived at 10K, which is not totally beyond the pale, just on the way to being a fairly plump htaccess.)

If you speak php, this is when you start looking into rewriting to a php script which does the lookup* and then issues the redirect. That's rewrite to the php, followed by a single redirect to the destination page, because g### gets huffy if a request is redirected more than once before arriving at its destination.

:: memo to self: get off my ###, uncheck that canonical-domain-name box in host's control panel, and add ONE BLASTED RULE to htaccess instead ::

* Lookup where? On that same php page, or an external database? Could swear I asked about this just recently...


 1:02 am on Feb 10, 2013 (gmt 0)

msg:4542925 describes the "rewrite to PHP file that then issues the redirect" method.

 10:17 pm on Feb 10, 2013 (gmt 0)

Ok I took the lazy way out. If I could write a php script to talk to my htacces and all that jazz I would. Unfortunately I cannot. So I can only operate within my skill set.

I ended up using this one liner to direct all files to my new root. If I'm doing this, is this a good way to do it? Or will/can cause issues or other? Thanks!

RewriteRule (.*) http://www.example.com/ [R=301,L]


 10:26 pm on Feb 10, 2013 (gmt 0)

The PHP script needs an array with the URL data in pairs, some sort of query to get the new URL from the array, the HEADER directive to send the redirect response, and a catchall to send a 404 response if the array lookup failed.


 10:27 pm on Feb 10, 2013 (gmt 0)

Ok I took the lazy way out. If I could write a php script to talk to my htacces and all that jazz I would. Unfortunately I cannot. So I can only operate within my skill set.

I ended up using this one liner to direct all files to my new root. If I'm doing this, is this a good way to do it? Or will/can cause issues or other? Thanks!

RewriteRule (.*) http://www.example.com/ [R=301,L]


 11:15 pm on Feb 10, 2013 (gmt 0)

... so if a search-engine robot comes along and asks for an isolated image file, they'll be told to go to the front page of the new domain?

At a minimum, constrain your redirect to pages --including directories ending in / slash-- and serve up a 410 for the others.

You don't need the parentheses, since nothing is being captured for later reuse.

The php script doesn't communicate with htaccess. (It can, but here it doesn't.) It simply picks up pages and tells them where to go.

For, uh, a given definition of "simply" ;)


 3:52 pm on Feb 11, 2013 (gmt 0)

You guys keep telling what to do but don't show me how to do it.

So should my snippet look like this then?
RewriteRule .* http://www.example.com/ [R=301,L

And how should it look with targeting only files? Thanks


 8:13 pm on Feb 11, 2013 (gmt 0)

You guys keep telling what to do but don't show me how to do it.

Yup. That's how THIS forum works.

:: snrk ::

If you want ready-to-serve answers, there's That Other Forum.* On balance, their answers seem to be correct more than 50% of the time.

* If you don't know which one I mean, don't ask.

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