Michael
Michael

Reputation: 402

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.

What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.

However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:

legacydomain.com/something.html to ourdomain.com/legacydomain/something.html

legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html

This is what I have tried:

RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]

Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html

It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.

Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".

Upvotes: 1

Views: 39

Answers (2)

Wige
Wige

Reputation: 3918

For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.

RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]

Then you would use a catch-all for everything else.

RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]

Upvotes: 1

Wige
Wige

Reputation: 3918

Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.

Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html

Then, simply redirect everything else to the base URL.

RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/

These must be put in your .htaccess file before the RewriteEngine on statement.

Upvotes: 0

Related Questions