4

I actually posted about this years ago, never got anywhere. Did more research, did testing, got something I thought worked and mostly did, but it seems to get bogged down in infinite loops under certain circumstances. I am at my wit's end. Anyone who can help me resolve this, you are my hero for life. :)

The Problem

I run a photo gallery and I changed my website structure a few years ago.

The Old Site

The old structure, an album had a form like this:

http://desktopscenes.com/Name Of Album (Year)/default.html

Individual photo slides were like this:

http://desktopscenes.com/Name Of Album (Year)/slides/Name Of Slide.html

And the actual slide was:

http://desktopscenes.com/Name Of Album (Year)/slides/Name Of Slide.jpg

The New Site

An album is like this:

http://desktopscenes.com/Name_Of_Album_-_Year/index.htm

A photo slide like this:

http://desktopscenes.com/Name_Of_Album_-_Year/slides/Name_Of_Slide.htm

And the photo:

http://desktopscenes.com/Name_Of_Album_-_Year/slides/Name_Of_Slide.jpg

So What I Need to Happen Is:

  1. Change all spaces to underscores.
  2. Change the year references from " (Year)" to "_-_Year".
  3. Change "html" to "htm".
  4. Not break anything else. :)

My Attempt

You'd think this would be fairly simple. And in fact I actually got this to work -- most of the time. The problem is that occasionally something sends a request and sends it into an infinite loop.

Here's my code.

RewriteEngine On
Rewritecond %{HTTP_HOST} ^(desktopscenes.com|www.desktopscenes.com)$ [nc]
RewriteRule ^(.*)\s(.*)$ $1_$2 [N,NE]
RewriteRule ^(.*)\((.*)$ $1-_$2 [N,NE]
RewriteRule ^(.*)\)(.*)html$ http://desktopscenes.com/$1$2htm [NE,R=301,L]
RewriteRule ^(.*)\)(.*)jpg$ http://desktopscenes.com/$1$2jpg [NE,R=301,L]

A Clue - Revised!

If 'proper' requests come in as they should, this works fine. I think the problem is if a request comes in that is incorrect. In particular, sometimes I get a request like this on the old site:

http://desktopscenes.com/Name Of Album (Year)/Name Of Slide.jpg

Notice the "slides" folder is missing. I am pretty sure this request created an infinite loop.

ETA: Looking through my logs I am now seeing this is much more common than I thought. I would love a way, if possible, to add the "slides/" folder back in if a request for a .jpg comes in without "slides/" in the URI.

In Closing

Thank you for listening and attempting to help me with this. It has literally been bothering me for years!

Addendum

I have also been changing apostrophes in the original names to dashes. But these are infrequent so they are less important.

Revised Code

This is what I have now

RewriteEngine On
Rewritecond %{HTTP_HOST} ^(desktopscenes.com|www.desktopscenes.com)$ [nc]
RewriteRule ^(Test.*)\s(.*)$ $1_$2 [N,NE]
RewriteRule ^(Test.*)\((.*)$ $1-_$2 [N,NE]
RewriteRule ^(Test.*)\)(.*)/slides/(.*)html$ http://desktopscenes.com/$1$2/slides/$3htm [NE,R=301,L]
RewriteRule ^(Test.*)\)(.*)/slides/(.*)jpg$ http://desktopscenes.com/$1$2/slides/$3jpg [NE,R=301,L]

This handles the case where I have album (year)/subalbum/slides.html or whatnot.

The "Test" is in there so I can test this on a dummy structure without blowing up the rest of the site.

I am still not sure how to handle requests of the form: http://desktopscenes.com/Name Of Album (Year)/Name Of Slide.jpb

That is no "slides". I am not sure what the regex is to NOT find a particular string, I guess I could look that up.

Another Update - Adding back "/slides/"

I don't need to search for "no slides". Since the ones that match "slides" have the "L" suffix they should stop processing once found. (Correct?)

So I was able to add this:

RewriteRule ^(Test.*)\)([^/]*)/(.*)jpg$ http://desktopscenes.com/$1$2/slides/$3jpg [NE,R=301,L]

Which actually adds the "/slides" back in if not present on a JPG request, which is great!

When I tried to do the same for HTML however:

RewriteRule ^(Test.*)\)([^/]*)/(.*)html$ http://desktopscenes.com/$1$2/slides/$3htm [NE,R=301,L]

It doesn't work. Any idea why?

  • 1
    I especially like the not break anything else... – closetnoc Sep 15 '15 at 21:38
  • 2
    Your current rules don't appear to handle "an album" (ie. default.html to index.htm)? Presumably you already have a canonical www to bare domain redirect at the top of your script? If so, your first RewriteCond would seem to be superfluous (but this would only apply to the first RewriteRule anyway). – MrWhite Sep 15 '15 at 21:38
  • 1
    @w3d is our local Guru here on this stuff- you are in good hands!! – closetnoc Sep 15 '15 at 21:40
  • 1
    I would perhaps consider solving this in a server-side script (PHP?) rather than .htaccess (even as part of your custom 404)? Particularly since this is just to handle old (outdated) links to your site. Solving this in .htaccess feels more academic than practical, however, it is an interesting problem. – MrWhite Sep 15 '15 at 21:57
  • Thanks for the replies. This is something I am trying to clean up that I should have dealt with years ago. Two things I noticed, I need to put a reference to "/slides" in there to prevent the problem with requests that have that missing. I also need to handle cases where my photos are two levels deep. I think I have figured that stuff out. I am using commercial software (jAlbum) and am not sure how I would be able to put PP in here. I just added "hard-coded" redirects ("Redirect permanent") for the albums since that seemed simplest. Thanks. – Charles Kozierok Sep 15 '15 at 22:50
0

I'm going to close this. The code basically works, there's a more specific question someone may be able to help with, without having to wade through all my rambling.

Thanks for the patience of whoever read this and tried to help.

Not the answer you're looking for? Browse other questions tagged or ask your own question.