Mobile app version of vmapp.org
Login or Join
Vandalay111

: Need expert help with tricky redirect problem 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

@Vandalay111

Posted in: #ModRewrite #UrlRewriting

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:
desktopscenes.com/Name Of Album (Year)/default.html


Individual photo slides were like this:
desktopscenes.com/Name Of Album (Year)/slides/Name Of Slide.html


And the actual slide was:
desktopscenes.com/Name Of Album (Year)/slides/Name Of Slide.jpg


The New Site

An album is like this:
desktopscenes.com/Name_Of_Album_-_Year/index.htm

A photo slide like this:
desktopscenes.com/Name_Of_Album_-_Year/slides/Name_Of_Slide.htm

And the photo:
desktopscenes.com/Name_Of_Album_-_Year/slides/Name_Of_Slide.jpg

So What I Need to Happen Is:


Change all spaces to underscores.
Change the year references from " (Year)" to "_-_Year".
Change "html" to "htm".
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 desktopscenes.com)$ [nc]
RewriteRule ^(.*)s(.*)$ _ [N,NE]
RewriteRule ^(.*)((.*)$ -_ [N,NE]
RewriteRule ^(.*))(.*)html$ desktopscenes.com/htm [NE,R=301,L]
RewriteRule ^(.*))(.*)jpg$ desktopscenes.com/jpg [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:
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 desktopscenes.com)$ [nc]
RewriteRule ^(Test.*)s(.*)$ _ [N,NE]
RewriteRule ^(Test.*)((.*)$ -_ [N,NE]
RewriteRule ^(Test.*))(.*)/slides/(.*)html$ desktopscenes.com//slides/htm [NE,R=301,L]
RewriteRule ^(Test.*))(.*)/slides/(.*)jpg$ desktopscenes.com//slides/jpg [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:
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$ desktopscenes.com//slides/jpg [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$ desktopscenes.com//slides/htm [NE,R=301,L]


It doesn't work. Any idea why?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Vandalay111

1 Comments

Sorted by latest first Latest Oldest Best

 

@Chiappetta492

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme