Forum Moderators: phranque

Message Too Old, No Replies

Looking for advice for redirects.

         

Gemini23

9:00 pm on Mar 22, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, I am looking for advice regarding redirects....

Website has a main domain and also add-on domain
previous redirects used were as follows... on main domain of example.com

/oldpage.html http://www.example.com/newpage.html

this works fine on the main domain but ALSO caused redirect - on the add-on domain - ie url of addondomain/oldpage.html to also be redirected to http://www.example.com/newpage.html (which is NOT wanted)

The redirect in the .htaccess has been in the format
Redirect 301 /oldpage.html http://www.example.com/newpage.html

There are about 50 redirects required on the main domain and none on the add-on domain.

Suggestions as to how exactly to write the code (for the multiple urls) for the redirects IF this is not correct - Redirect 301 /oldpage.html http://www.example.com/newpage.html

Thanks in advance....

[edited by: bill at 1:05 am (utc) on Mar 23, 2016]
[edit reason] use EXAMPLE.COM to prevent auto-linking [/edit]

lucy24

2:55 am on Mar 23, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The bad news is: You're going to have to get rid of any and all redirects that use mod_alias (Redirect or RedirectMatch by that name) and change them all to mod_rewrite.

The good news is: It's easy. If you have lots of them, you can run a couple of global search-and-replaces in any text editor.

It sounds as if all your redirects are located in the outer htaccess, where they are seen both by the "primary" and the "addon". In the worst-case scenario, each separate redirect will now require a Condition, like this:
RewriteCond %{HTTP_HOST} example\.primary
RewriteRule oldurl http://www.example.primary/newurl [R=301,L]
... but let's look at alternatives.

When you say "about 50" do you mean 50 entirely different redirects, following no pattern, so you have to write 50 separate rules? Or can you collapse them into just a few rules? Is there any relationship between old URL and new URL?

If you really do have to make fifty separate rules, it may be time to bring out the rarely used [S] flag, meaning "skip so-and-so-many following rules". For example:

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.)

If you have, or plan to acquire, multiple add-on domains, replace "example.addon" with "!example.primary", meaning "any hostname other than the primary".

But once you've changed from mod_alias to mod_rewrite, you don't really need to do either of these things (the [S] version or the [L] version). If your addon domain has its own htaccess with its own RewriteRules, the earlier rules will simply be ignored as if they never existed.

Replace example.primary and example.com with the actual names of your domains. For posting purposes, it has to be example.tld -- but the tld can be anything, so "primary" and "addon" are obviously the easiest to remember.

:: wait, stop, rewind ::

Why are those addon URLs getting requested at all? Who's asking for example.addon/url-that-never-existed-on-this-site? Is it just bad luck that the primary and the addon happen to have some pages with the same name, where one set is to be redirected while the other set remains unchanged? Or is there something you're not telling us?

[edited by: bill at 12:54 am (utc) on Mar 24, 2016]
[edit reason] fix code [/edit]

Gemini23

12:47 pm on Mar 23, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the detailed reply Lucy - I have now looked at all of the urls that need redirecting.
There are 175

Essentially a co.uk domain that had been built with Dreamweaver/express Web creating .html urls was changed to a Wordpress installation. There are widespread backlinks to the website hence the need for redirecting (but I will have to double check to see IF all of those redirects are needed.

Answering your last question first - 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.
So essentially it is only one (unless I can find any more but unlikely to be more than one or two as even contact page has a different url - but I will check)

The .htaccess used is the one in the main public.html which is I guess why it is affecting all.

Given the above information would you suggest just changing the relevant affected urls?

ie by adding
RewriteCond %{HTTP_HOST} example\.primary
RewriteRule oldurl http://www.example.primary/newurl [R=301,L]

could you explain the code as to how to redirect the file extension of co.uk as I am not entirely clear on that.
ie with exampledomain.co.uk/gallery.html to redirect to newdomain.co.uk/gallery/

Many thanks

lucy24

8:27 pm on Mar 23, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Careful! There were two near-lethal typos in my previous post, so don't do any cutting and pasting. At least until we're sure they've been fixed. :(

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.

Then you don't need a condition most of the time, because nobody will ever be asking for example.one/url-that-only-ever-existed-on-example.two.

exampledomain.co.uk/gallery.html to redirect to newdomain.co.uk/gallery/

This is the one that exists on both sites, and you only want to redirect one of them? That's where the condition comes in.

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.

But wait! Are all your redirects in the form
/someurl.html
changing to
/someurl/
? This can absolutely be done universally. If it applies to all URLs on the site, you'd make a single rule. The easiest form is if your URLs don't happen to contain any literal periods. (Periods are perfectly legal-- see, for example, all those directories at apache dot org called /2.2/ and similar --but it's a heck of a lot easier if you can avoid them.) It would then look like this:

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.

:: wandering off to check raw logs and see just how often this kind of request comes in on modern sites ::

Edit: In the "pattern" part of a RewriteRule (the part on the left), and in any RewriteCond, the literal period . should be escaped as \. Most of the time this is a non-lethal error, but it's better to stick with the habit. In other words: Do as I say, not as I do.

Gemini23

9:47 pm on Mar 23, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sadly the characters in the redirects are not all the same...

ie not all from
oldurl.html redirect to oldredirect/

so no they are not.... ( a quick sacn through I would say maybe 10% are...)
/someurl.html
changing to
/someurl/

lucy24

10:02 pm on Mar 23, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



maybe 10% are

You can collapse those, at least, into a single rule. It would look like this:

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.

See the parentheses? Here they are handily doing two unrelated jobs: grouping a list of pipe-separated words that all have ".html" after them, and then also capturing whichever member of the list they happen to find.

You did say / at the end of the new URL, right? I think I forgot that before. But this in turn reminds me that another thing search engines like to do is ask for
/directory
when the correct form is
/directory/
Again it's a kind of entrapment: they're looking for Duplicate Content. (bing seems to be especially fond of Technical Quality issues.) If the URL represented a real, physical directory, it would not be an issue, because mod_dir steps in and issues a redirect as needed. If you're using a CMS, make sure you've clicked the appropriate setting so only one form-- either with or without final slash-- serves content, and the other form gets redirected. It's still better if you can code the redirect yourself to save the server some work. But that's another thread.

:: wandering off again to see how often legitimate search engines* have recently asked for "/directory" and-that's-all ::


* Some minor search engines seem to be especially stupid about final slashes, and always ask for the wrong form.

Gemini23

10:07 pm on Mar 23, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the trailing slash seems to be standard for wordpress (the new pages).... on all posts and pages (these are all pages)