Forum Moderators: phranque

Message Too Old, No Replies

Help to redirect old URL to new one

         

luismartin

9:24 am on Jul 5, 2012 (gmt 0)

10+ Year Member



I'm not being able to get this working. There are a lot of referers to our old web that we would like to redirect to the new one. In fact our error.log file gets a lot of lines like this: "Request exceeded the limit of 10 internal redirects due to probable configuration error.".

This is the old link:
www.example.com/websonia/jsp-sonia/index.jsp

and the new one is this:
www.example.com

I'm trying to do a redirect with .htaccess (which is on the web root) with these lines:

RewriteCond %{REQUEST_URI} ^(.*)jsp(.*)$
RewriteRule ^(.*)$ http://www.example.com/index.php [R=301]


with no success. There is no "websonia" directory anymore in the new web.

What am I doing wrong? Thanks in advance!

[edited by: incrediBILL at 7:56 am (utc) on Jul 7, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

lucy24

9:53 am on Jul 5, 2012 (gmt 0)

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



There are a lot of referers to our old web that we would like to redirect to the new one. In fact our error.log file gets a lot of lines like this: "Request exceeded the limit of 10 internal redirects due to probable configuration error."

I hope you're not suggesting that there is a connection between these two sentences. The errors mean that you have made a big mistake in a rewrite, so something goes into an infinite loop and the server has to put a stop to it. It has nothing to do with the number of requests; that's an entirely different error message.

For starters, the Redirect needs an [L]. If you are lucky, that will put a stop to the infinite redirects.

Aside from that... no two lines of your post seem to say the same thing. Are you changing domains? Shortening URLs? Redirecting everyone to the front page? (Nooooo!) Changing extensions? When you say "link" do you mean URL? When you say "new web" do you mean "new domain" or "newly designed site" or something else?

FIRST clarify what you want to do. THEN you can start hammering away at the code.

luismartin

3:03 pm on Jul 5, 2012 (gmt 0)

10+ Year Member



Hello Lucy, problem solved, thank you! I had to add the [L] to the redirect, like you said, so the lines are like these:

RewriteCond %{REQUEST_URI} ^(.*)jsp(.*)$
RewriteRule ^(.*)$ http://www.example.com/index.php [R=301,L]


As for the questions you asked me: It is the same domain. The previous web was physically located in another directory of the same unit and it used the "physical" subdirectory "websonia". It was made in Java while the new one is PHP. I was trying to perform a 301 redirection from the frontpage of the old website to the frontpage of the new one (why that "noooo!"?). I think that's all. ;)

[edited by: incrediBILL at 7:56 am (utc) on Jul 7, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

g1smd

4:29 pm on Jul 5, 2012 (gmt 0)

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



That code has a number of serious problems still built in.

^(.*)jsp(.*)$
is garbage. Dump the
^(.*)
and the
(.*)$


However, there's no need for a condition at all. You can put the RegEx pattern in the Rule itself.

^(.*)$
simplifies to
.*


Do not redirect to a named index page. The canonical URL for the site root is the domain name followed by a trailing slash.

RewriteRule jsp http://www.example.com/ [R=301,L]


However, your code potentially is for multiple URLs to redirect to the home page. This is nearly always a Very Bad Idea. If it's just a few old pages, this will be fine.

lucy24

7:54 pm on Jul 5, 2012 (gmt 0)

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



RewriteRule jsp http://www.example.com/ [R=301,L]

Typo for
RewriteRule \.jsp$

et cetera? ;)

(why that "noooo!"?)

Had to make sure you weren't redirecting an entire site :)

If you're only redirecting the specific page

websonia/jsp-sonia/index.jsp

then its full exact name can go into the Pattern (left side) of the Rule, with \. escape and $ anchor as appropriate.

And once you've canonicalized any and all index pages to / (trailing slash, with no "index.whatever") you can change extensions every other week and never have to worry about redirecting again.

luismartin

11:36 pm on Jul 6, 2012 (gmt 0)

10+ Year Member



Hello Lucy, excuse my ignorance about this matter, but I'm not sure if I understood your last post. Please tell me again as if I had no idea. I actually have very little knowledge about Apache, most of it regarding the mod_rewrite module.

If I'm not wrong you meant to say that I could replace this:

RewriteCond %{REQUEST_URI} ^(.*)jsp(.*)$
RewriteRule ^(.*)$ http://www.example.com/index.php [R=301,L]


for just this:

RewriteRule \.jsp$ http://www.example.com/ [R=301,L]


No need of a conditional before the rule. Isn't it?

[edited by: incrediBILL at 7:57 am (utc) on Jul 7, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

lucy24

1:31 am on Jul 7, 2012 (gmt 0)

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



Well, not quite, but partly. Are you redirecting only one specific page, or are you redirecting everything that has the .jsp extension?

mod_rewrite works on a "Two steps forward, one step back" principle. It looks at each Rule in order. If the Rule might potentially apply-- for example if there is a request for example.com/doodah.jsp and the Rule says \.jsp$ --then it looks at the Conditions. If the Rule can't apply-- for example if the request is for example.com/stylesheet.css and the Rule says \.jsp$ --it never even looks at the Conditions.

If you are doing a straight Redirect from one URL to another, there may not need to be any Conditions at all. For example, with the single page

www.example.com/websonia/jsp-sonia/index.jsp

If there are no Rewrites involved, then all you need is the single line

RewriteRule ^websonia/jsp-sonia(/(index\.jsp)?)?$ http://www.example.com/ [R=301,L]

The part inside the parentheses is to cover alternative request formats, including

www.example.com/websonia/jsp-sonia/

which is actually the correct name of the old URL. Since you are redirecting rather than rewriting, it is OK to collapse multiple forms of the same name into a single Rule.

Now, if you wanted to redirect everything with a .jsp extension, then you'd have to spell things out more exactly. Details will depend on your specific site: how many files are involved, is there a consistent pattern to the renaming and so on.

luismartin

9:03 am on Jul 7, 2012 (gmt 0)

10+ Year Member



I was wrong when I thought that conditions were parsed prior to their rules. I always though it was that way.

Well, according to the errors stored in in erros.log, all the links to our old web had the same URL (www.example.com/websonia/jsp-sonia/index.jsp) with an additional and optional URL variable for languages (?I=FR). I didn't build the old site, a terrible and ugly flash-navigation-based web BTW. If I'm not wrong, as everything happened within flash, the URL was always the same except when changing language, in which case the page was reloaded including the corresponding URL variable. In short, I just want to redirect all the old URL's to the frontpage of the new one.

So, as you suggested, I understand that something like this should be enough:

RewriteRule \.jsp$ http://www.example.com/ [R=301,L] 


or even this (the "websonia" segment is not used at all in the new web):

RewriteRule ^websonia http://www.example.com/ [R=301,L] 


Is this correct?

g1smd

10:06 am on Jul 7, 2012 (gmt 0)

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



Both are correct, but the latter will be much more efficient especially when the request is for other pages or images on the new site. In that case the rule will be skipped after parsing only the first few characters, rather than having to parse to the very end. Every rule is parsed for every request, so the sooner any non-matching rule can be discarded the better.

luismartin

10:30 pm on Jul 9, 2012 (gmt 0)

10+ Year Member



ok g1smd. Thanks for your tip!