Forum Moderators: phranque

Message Too Old, No Replies

redirecting on "if filename includes"

         

hih2839

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

10+ Year Member



Is there anyway in modrewrite to redirect someone based on if the url matching a file name. For example.

Someone types in:
www.domain.com/folder/01

and it redirects them to
www.domain.com/folder/01-hello.html

So the idea is redirect to the first file which includes this in the filename within this folder.

I've tried and tried to find this but can't find anything.

Any help would be appreciated. :)

g1smd

12:13 am on Apr 11, 2012 (gmt 0)

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



Mod_rewrite doesn't look at the filenames on the server.

It examines what is in the URL request arriving from the browser.

The rule then targets a specific filename on the server.

Another apache process then attempts to fetch it. If it exists, you see the content. If not, the server sends the 404 response.

The only way to do what you want to do, is to rewrite (that's rewrite, not redirect) the request to a specific PHP script that itself scans the hard drive folder, selects a particular file and returns it.

Please use example.com in this forum to stop the auto-linking process from kicking in.

[edited by: g1smd at 1:01 am (utc) on Apr 11, 2012]

lucy24

12:30 am on Apr 11, 2012 (gmt 0)

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



Well, what have you tried so far? Matching the requested url may be the easiest rewrite to do-- conceptually, that is-- because everything goes in the Rule and you don't have to mess with Conditions. In fact it's the kind of thing Apache wants you to do with mod_alias instead. (Easy for them to say. They've got their own server so they don't have to worry about which module will execute before which.) It is not often that you get to tell people to disregard Apache's advice :)

Step One is to lay out very carefully, in English, exactly what you want to do. Once you've got that fine-tuned, the solution should fall into place.

Your question is a little fuzzy. Do you want to match one specific bit of text, or any request, or any request within a particular folder, or...

Remember, you know exactly what you want to do, but we don't. And even more important: mod_rewrite doesn't know what you really mean. It only knows what you tell it. So you have to be very careful to say everything you mean and nothing you don't mean. See, for example, a recent thread in this forum called "What a difference a slash makes." Oops. Darn it, mod_rewrite, you know that's not what I meant!


Oh, and...

Somewhere around the top of this page there are links to the Forum Library-- which in turn has links to assorted Apache pages-- and the Forum Charter-- which will tell you to use www.example.com in all your, ahem, examples. This is not just to keep from naming your own domain. It's because the Forum software automatically converts anything with http into a clickable link, and then we can't see what you wrote. Only "example" is exempt.

hih2839

12:52 am on Apr 11, 2012 (gmt 0)

10+ Year Member



the idea is that
www.domain.com/folder/01

will always go to the file with 01 in the filename.

The last part of the filename may change
the -hello part of 01-hello.html

the php idea may be the best way.

My php skills are not to that level though. Any chance you guys could help me on the php script? I think i can handle the modrewrite

g1smd

1:01 am on Apr 11, 2012 (gmt 0)

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



Please use example.com in code examples in this forum.

The PHP forum is a few doors along from this one. It does have a common purpose - to get your code working.

lucy24

8:59 am on Apr 11, 2012 (gmt 0)

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



Oh, wait. You're not asking about a simple redirect at all, are you? You're looking for something like mod_speling on steroids, or mod_negotiation cranked up several levels.

btw, in case you can't tell: I type slow, so I didn't see g1's reply while I was composing mine. And I don't know what he changed in the edit ;)

You're saying: if a user requests

www.example.com/folder/ab

you want to look for anything that might happen to start with "ab..." ?

Browsers can do it without batting an eye-- but only if the person has already been there so all they have to do is autocomplete.

Otherwise, that's way beyond Apache. You're in php territory. Unless there's a backstory you're not telling. Have you changed filenames recently, or do you happen to have a lot of users who are lousy typists?