Forum Moderators: phranque

Message Too Old, No Replies

Redirect a lot of logical urls with subdirectories to differen new url

         

torepeng

8:52 pm on Jul 18, 2014 (gmt 0)

10+ Year Member



Hi -

I have a problem. I want to redirect a "lot of urls" to another site. (splitting a website in two, and redirect a lot of old logical urls indexed by google to the new site)

Like this -

Redirect permanent /nor/public http://example.com/index.php?ID=1
Redirect permanent /nor/public/subdirectory http://example.com/index.php?ID=2
Redirect permanent /nor/public/subdirectory2 http://example.com/index.php?ID=3
Redirect permanent /nor/school/about http://example.com/index.php?ID=4
Redirect permanent /nor/school/about/contact http://example.com/index.php?ID=5
Redirect permanent /nor/school/about/demo http://example.com/index.php?ID=6
.. and so on..

But the redirect for line 2 and 3 will redirect like this:

http://example.com/index.php?ID=1/subdirectory

and not to

http://example.com/index.php?ID=2

Is there an simple solution to help me out? I have a script that gives me all the urls to redirect... and I don't have a lot of time :(

Regards
Tore Petter

lucy24

9:31 pm on Jul 18, 2014 (gmt 0)

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



Crucial question:
All the quoted rules lead to redirects from a simple, straightforward-- but longish-- URL to a new URL that's shorter but ends in "index.php?query=blahblah". Is it possible you've got your patterns and targets mixed? The rules really look more like what you'd expect in a behind-the-scenes rewrite, not an external redirect.

The specimen rules use mod_alias syntax ("Redirect" by that name). But mod_alias can't see query strings. For that you need to use mod_rewrite (RewriteRule). This, in turn, means that you need to convert all your existing mod_alias redirects to mod_rewrite syntax. There are posts telling you how to do this globally if you've got a lot of them.

afaik, you can use mod_alias to redirect to something containing a query string. But I suspect there's more involved, so you will need mod_rewrite in the end.

torepeng

10:06 pm on Jul 18, 2014 (gmt 0)

10+ Year Member



Thanks for the answer.. but the redirect I want to do is what you see..

I have two identical cms up and running (with the same names and IDs).. and want to split them in an external site, and an internal site. So the logical url on site A;
siteA.com/nor/school/about i want to redirect to siteB.com/index.php?ID=4 (and I have a modrewrite there, so it will convert it to the right logical url there)

I have multiple nodes (threes) with a lot of different "subdirectories" recursive (logical urls made on siteA) and I have written a script so I can get one line per node with both the logical url to redirect and the ID i intend to redirect to on the other site. My plan was to have multiple files like for instance rewrite_topnode1324.conf (with a line per redirect for the hole nodethree) in conf.d..

siteA siteB (ID)
/nor/school/about 5
/nor/school/about/contact 6
/nor/school/about/demo 7
... and aprox 150+ different nodes, with different "directory names) on siteA.. So I have to do this automatically..

I cant use Redirect permanent siteA/index.php?ID=5 siteB/index.php?ID=5, since the locical url is the address that is indexed by the search engines..

Is the only solution to make a rule to take all /nor/school* (and all subdirs) and redirect them to one ID? How do I do that?

Thanks so far.

penders

10:50 pm on Jul 18, 2014 (gmt 0)

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



But the redirect for line 2 and 3 will redirect like this:

http://example.com/index.php?ID=1/subdirectory


With mod_alias redirects you will need to specify them in order of specificity, most specific first. The URL-path you are matching is a prefix (whole path segments), so your first rule (/nor/public) will match "/nor/public/subdirectory". And everything after the match is appended to the end of the destination URL.

...to a new URL that's shorter but ends in "index.php?query=blahblah". Is it possible you've got your patterns and targets mixed?


So the logical url on site A;
siteA.com/nor/school/about i want to redirect to siteB.com/index.php?ID=4 (and I have a modrewrite there, so it will convert it to the right logical url there)


Yeah, that doesn't sound quite right. Shouldn't you be redirecting to the "logical URL" - the public URL that users access, search engines index and which should appear in the browsers address bar? Or is "siteB.com/index.php?ID=4" that URL?

torepeng

11:31 pm on Jul 18, 2014 (gmt 0)

10+ Year Member



Hi!

I started to look at RewriteRule, but then I couldn't redirect to the ID.. I have to redirect to the ID.. because the locical url is different on SiteB from SiteA, but the ID is the same (the logical url is relative based on what part of the three it is.. it can be moved around, and then the logical url changes)

BUT - you guys solved it.. because i can change the order of the lists on my redirects...

Like this:

Redirect permanent /nor/public/subdirectory http://example.com/index.php?ID=2
Redirect permanent /nor/public/subdirectory2 http://example.com/index.php?ID=3
Redirect permanent /nor/public http://example.com/index.php?ID=1
Redirect permanent /nor/school/about/contact http://example.com/index.php?ID=5
Redirect permanent /nor/school/about/demo http://example.com/index.php?ID=6
Redirect permanent /nor/school/about http://example.com/index.php?ID=4

Then the specific is first in order.. and then the parent folder stands last.. Then I think it will work :-)

Thanks a lot!

penders

12:32 am on Jul 19, 2014 (gmt 0)

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



Almost... "/subdirectory2" should come before "/subdirectory".

I have to redirect to the ID.. because the locical url is different on SiteB from SiteA, but the ID is the same


Hhmmm, nope, don't get it. ;)

Not intending to press this too much, but... are you using the ID in order to generate your redirects on Site A or something? What is a "locical" URL (you've used that term twice now) and is this different to a "logical" URL? In fact, what is a "logical" URL? (I tend to think in terms of "user-friendly" (or SEF) URL and "actual"/underlying URL.)

torepeng

12:53 am on Jul 19, 2014 (gmt 0)

10+ Year Member



Sorry about the spelling error.. I meant logical url or user friendly url..

And I have solved it.. My original script outputted first:

/nor/directory
/nor/directory/subdirectory
/nor/directory/subdirectory/something
/nor/directory/subdirectory2
/nor/directory/subdirectory2/somethingelse

But i just did an reverse sort on the array who generate this three so that the most spesific url is on top, and the base url (or node) is at the bottom.. And yes -the ID is used to generate the redirects.

It is a little difficult to explain.. but we of course dont use ID in urls (but it works).. Because we have a ModRewrite which converts index.php?ID=1 to an logical url (SEF) on the fly if that is used in the browser. The logical url is based on where the content is placed in the three structure. So if we move the node "school" from /nor/student/school/* to /nor/staff/school/*, it will get a new url. But if I used the ID in my redirect, I will always get the right page. This will only be used for a period of time until all is indexed and we have seperated our two CMS..

lucy24

1:10 am on Jul 19, 2014 (gmt 0)

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



Further issue: If you've got a CMS, then you are already using mod_rewrite. Are the redirects you're asking about located in a completely different place, where requests for the old URL-- to be handled with mod_alias-- will never requests for the new URL-- to be handled with mod_rewrite? Here "located" means physical location, not URL.

Hhmmm, nope, don't get it.

What he said. :) At the very least, it means that every request will be redirected twice. And I honestly don't understand why this would be necessary.

so it will convert it to the right logical url there

This, too, makes me uneasy. It puts me in mind of g1smd's recurring line about how mod_rewrite can't "make" an* URL.

Don't know about the rest of youse, but to me "logical URL" implies the actual, physical location of the content. In other words, the very thing a CMS-- or any internal rewrite-- is set up to conceal.


* Yes, I do know how it's supposed to be pronounced. But in my mind it will always rhyme with "Duke of URL".

penders

1:11 am on Jul 19, 2014 (gmt 0)

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



Ah OK, thanks for the follow up, I think I see what you're saying now. The user will get to the correct "user friendly" URL eventually (after 2 redirects - does this affect indexing?), but otherwise you don't necessarily know what the correct SEF URL is from Site A. (Although Site B knows the correct URL from the ID, so why doesn't Site A? Presumably the Site A redirects would perhaps need updating periodically - and this isn't possible?)

torepeng

9:45 am on Jul 19, 2014 (gmt 0)

10+ Year Member



lucy24 - Yes. The redirects I was talking about are located on different places. They was identical, but when we restructure content, they will get a new logical url (SEF). So therefor, it is better to redirect to the ID of the page, and always get the right page. Its an external CMS, not homemade :-), so I cant explain directly how they do it, but if you write a url like this example.com/index.php?ID=44 it will become for instance example.com/nor/school/project in the browsers url.. Both Site A ans Site B go "different ways now", so this redirects are only for the startup. So A can be different from B if we speak of logical urls, but the ID will be the same.

Example - An employee want to find SiteA.com/nor/about/internal/logo and have bookmarked this url.. This page has moved to SiteB.com/nor/employee/about/logo . The only way I can redirect to that new page is to redirect to the ID.. For on SiteA, I only now the url, not the new path on SiteB.

lucy24

6:51 pm on Jul 19, 2014 (gmt 0)

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



The redirects I was talking about are located on different places. They was identical

Sorry, no, you've misunderstood what I meant by "place" or "location". No relation to what Apache calls "location" which really means "part of URLpath". The question is whether the requests ever pass through the same physical directory, meaning that they are subject to the same htaccess file.

Are the two sites physically located on the same server and in the same userspace (or primary-domain directory or whatever the host uses) on that server?

if you write a url like this example.com/index.php?ID=44 it will become for instance example.com/nor/school/project in the browsers url

Watch out for that "it will become". The CMS does TWO things, which can be approximated as
RewriteCond %{THE_REQUEST} index\.php?ID=44
RewriteRule index\.php http://www.example.com/nor/school/project [R=301,L]

and then
RewriteRule ^nor/school/project /index.php?ID=44 [L]

The part I don't get is that you obviously know which "index.php?id=blahblah" corresponds to which public URL. So why not use the public URL in the first place? It also avoids the perils that go with making "index.php?ID=blahblah" visible to the world. Once this has happened, search engines will ask for it forever afterward, even if they have never met anything but a redirect.

:: idly wondering if there are also /dan/ and /sve/ directories ::

An employee want to find c . The only way I can redirect to that new page is to redirect to the ID.. For on SiteA, I only now the url, not the new path on SiteB.

You don't need to know the path. The CMS deals with that. But in all quoted rules, you are redirecting to a path. I simply don't understand why the rule can't say

RewriteCond %{HTTP_HOST} example\.com
RewriteRule ^(nor/)(about/)internal/(logo) http://www.example.no/$1employee/$2$3 [R=301,L]


where the condition is only necessary if old site and new site are located in the same physical directory. If they are in different places then no condition is not needed.