views:

274

answers:

5

Hi,

I'm trying to redirect pages from several old domains on one new domain on a page-to-page basis, that is:

page 1: http://www.example.org/default.asp?id=1 redirects to http://www.example2.org/newpage.html page 2: http://www.example2.org/default.asp?id=2 redirects to http://www.example.org/contact.html

...and so on. Every old page will redirect to a specific new page.

I have tried to write something in .htaccess along the lines of

Redirect 301 http://www.example.org/default.asp?id=1 h-tp://www.example.org/newpage.html
Redirect 301 http://www.example2.org/default.asp?id=2 h-tp://www.example.org/contact.html

But no luck so far. mod_alias is enabled...

I also tried using RewriteCond and RewriteRule like this:

RewriteCond %{HTTP_HOST} example.org
RewriteRule default.asp?id=1 http://www.example.org/newpage.html [NC,L,R=301]
...

In other words: I want to do a page-by-page redirect from id-based to alias-based along with a merge of three sites/domains into one site.

I hope there is a useful solution. Thanks in advance!

A: 

In httpd.conf:

RedirectPermananent URL1 URL2

randy melder
Unfortunately this method is not usable as I do not have access to the httpd.conf
Ulrik H. Kold
But all the examples I have found with Redirect does only work with in the same host.
Ulrik H. Kold
+1  A: 

The Redirect directive does only work with the URL path but not the host or query of the URL.

But it is possible with mod_rewrite:

RewriteCond %{HTTP_HOST} =example.org
RewriteCond %{QUERY_STRING} =id=1
RewriteRule ^default\.asp$ http://www.example.org/newpage.html [NC,L,R=301]

And as already said in the comments, you can use a rewrite map for the ID-to-alias mapping:

1 foo-page
2 bar-page
3 baz-page
…

The RewriteMap declaration (here a simple plain text file):

RewriteMap id-to-alias txt:/absolute/file/system/path/to/id-to-alias.txt

And finally the application:

RewriteCond %{HTTP_HOST} =example.org
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&?([^&].*)?$
RewriteCond ${id-to-alias:%3}&%1%4 ^([^&]+)&(.*)
RewriteRule ^default\.asp$ http://www.example.org/%1.html?%2 [NC,L,R=301]

That should also preserver the remaining query. If you don’t want that:

RewriteCond %{HTTP_HOST} =example.org
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&?([^&].*)?$
RewriteCond ${id-to-alias:%3} .+
RewriteRule ^default\.asp$ http://www.example.org/%0.html? [NC,L,R=301]
Gumbo
So basically I would need two lines per page?One with the RewriteCond matching the id and one with the RewriteRule making sure to send the user to the right page? And conclude with a [L]?Am I finally on the right path?
Ulrik H. Kold
In general, yes. But you can use a rewrite map to store the ID-to-alias mapping (see http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap).
Gumbo
And for the help of others: The RewriteMap declaration can only go into the httpd.conf - don't bother putting it into the .htaccess as I did just now.
Ulrik H. Kold
+1  A: 

A good alternative to mod_rewrite is to use whatever language you are most comfortable with and put the logic in your script. Point all URLs you'd like to redirect to your simple front controller.

RewriteRule ^default.asp$ /redirect.php [L]
RewriteRule ^another/path/to_some_file.ext$ /redirect.php [L]

And then, in redirect.php:

<?php
if ($_SERVER['SCRIPT_URL'] == '/default.asp'){
    $map = array(
        1 => '/newpage.html',
        2 => '/contact.html'
    );
    if (isset($_GET['id'])){
        $id = $_GET['id'];
        if (isset($map[$id])){
            header('HTTP/1.1 301 Moved Permanently', true, 301);
            header('Location: http://'.$_SERVER['HTTP_HOST'].$map[$id]);
            exit;
        }
    }
}
// ... handle another/path/to_some_file.ext here, or other domains, or whatever ...

// If we get here, it's an invalid URL
header('HTTP/1.1 404 Not Found', true, 404);
echo '<h1>HTTP/1.1 404 Not Found</h1>';
echo '<p>The page you requested could not be found.</p>';
exit;
?>
pwfisher
A: 

Just adding some more info

The OP's redirect directives are probably in the wrong format - from the relevant apache doc page:

The Redirect directive maps an old URL into a new one by asking the client to refetch the resource at the new location.

The old URL-path is a case-sensitive (%-decoded) path beginning with a slash. A relative path is not allowed. The new URL should be an absolute URL beginning with a scheme and hostname. Example:

Redirect /service http://foo2.bar.com/service

So that would make it:

Redirect 301 /default.asp?id=1 http://www.example.org/newpage.html

Hope that helps

jcinacio
But as the quoted extract states, `Redirect` does only work on the URL path and takes the given URL path as prefix that is to be matched.
Gumbo
A: 

I noticed that http://www.pangosoft.ca provides enterprise level website migration and redirection solution. Have a look at their page-to-page URLRedirector suite which can capture 404 and redirects to designated url.

Good luck!

jw