Forum Moderators: phranque
[edited by: bill at 1:05 am (utc) on Mar 23, 2016]
[edit reason] use EXAMPLE.COM to prevent auto-linking [/edit]
RewriteCond %{HTTP_HOST} example\.primary
RewriteRule oldurl http://www.example.primary/newurl [R=301,L]
... but let's look at alternatives. RewriteCond %{HTTP_HOST} example\.addon
RewriteRule (^|html|/)$ - [S=50]
followed by your fifty rules, without condition. Or you can rearrange things a little so, instead, it says RewriteCond %{HTTP_HOST} example\.addon
RewriteRule ($|html|/)$ - [L]
meaning "If the request is for a page in the addon domain, stop right here because the rest of my redirects won't concern you." (I wrote the rule to apply only to pages, so the server doesn't have to stop and evaluate conditions on non-page requests, where the rule won't apply anyway.) [edited by: bill at 12:54 am (utc) on Mar 24, 2016]
[edit reason] fix code [/edit]
There is ONE (and only one that I am aware of) url that has the exact same name on the add-on domain as on the main domain. (something generic like "gallery") There are not going to be any more add-on domains.
exampledomain.co.uk/gallery.html to redirect to newdomain.co.uk/gallery/
RewriteCond %{HTTP_HOST} example\.co\.uk
RewriteRule ^gallery\.html http://example.tld/gallery [R=301,L]
Note that in a redirect, it doesn't matter whether the target hostname is the same or different; you're spelling it out in full either way. RewriteCond %{HTTP_HOST} example\.co\.uk
RewriteCond %{REQUEST_URI} !index\.html
RewriteRule ^([^.]+)\.html http://example.tld/$1/ [R=301,L]
You can throw in some more Conditions if you need to exempt specific URLs, or fine-tune the body of the rule if it only applies in some directories. I put in the reference to "index.html" because search engines will ask for this periodically-- call it Entrapment-- and you obviously don't want things redirecting to example.com/directory/index/ like that. maybe 10% are
RewriteCond %{HTTP_HOST} example\.co\.uk
RewriteRule ^gallery\.html http://example.tld/gallery/ [R=301,L]
RewriteRule ^(oneurl|otherurl|thirdurl|etcetera)\.html http://example.tld/$1/ [R=301,L]
Since "gallery" is the only one that can exist in both places, I'd keep it as a separate rule. Otherwise the server has to evaluate a condition that will almost always succeed.