Question

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!

Was it helpful?

Solution

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]

OTHER TIPS

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;
?>

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

In httpd.conf:

RedirectPermananent URL1 URL2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top