Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite challenge

         

Swanny007

12:53 am on Mar 5, 2008 (gmt 0)

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



OK, I'm not quite a newbie when it comes to mod_rewrite. I've used it before with success after many hours of scouring the web for examples, etc. Anyway, I am a bit stumped right now.

Here's the facts...
- The *actual* web page URL is located at example.com/widgets/z00001.php
- I'm rewriting them to be more friendly example.com/z00001
- All links within the site link to the /z00001 format (so there shouldn't be any links to the *actual* page.

That works which is great.... However I want to disable people from accessing the actual location of example.com/widgets/z00001.php - I only want them to use the friendly URL and have them redirected from the actual file to the redirected URL (while still loading the actual web page behind the scenes).

Make sense?

Here's what I have so far:
RewriteRule ^z([0-9]+)$ widgets/z$1.php [L,NC]

I'm guessing I need another rule but I don't know how to do it. Can this even be done? Help!

Note: I tried this but it didn't work:
RewriteRule ^widgets/z([0-9]+)\.php$ http://www.example.com/z$1 [R=301,NC]

[edited by: Swanny007 at 12:56 am (utc) on Mar. 5, 2008]

g1smd

1:02 am on Mar 5, 2008 (gmt 0)

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



You need a RewriteCond preceding it.

There's a long thread from only yesterday with starting code that you'll need.

[webmasterworld.com...]

Swanny007

1:42 am on Mar 5, 2008 (gmt 0)

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



Thanks, that was the ticket. FYI here's the code that works:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /widgets/.*\ HTTP/
RewriteRule ^widgets/(.*).php$ http://www.example.com/$1 [R=301,L]

It redirects all pages from the widgets directory (not just the files that start with z) but that's good enough for me. I don't have an index.php file there either so if the next person looking at this code has an index.php in that folder, they'd need to find a workaround for that.

jdMorgan

6:21 am on Mar 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can speed this up a bit by making a few tweaks:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /widgets/[^.]+\.php[^\ ]*\ HTTP/
RewriteRule ^widgets/([^.]+)\.php$ http://www.example.com/$1 [R=301,L]

or perhaps just

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /widgets/[^.]+\.php
RewriteRule ^widgets/([^.]+)\.php$ http://www.example.com/$1 [R=301,L]

Either way, the pattern in the RewriteCond must exactly match the pattern in the RewriteRule (as far as that goes, and disregarding the parentheses used to create back-references) in order to prevent unexpected behaviour.

Jim

Swanny007

6:52 am on Mar 5, 2008 (gmt 0)

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



umm.... I don't quite follow you Jim...

I see you're using square brackets on the rewriterule line. How does that make it faster? I'm all for faster! I don't understand the difference between the curly & square brackets...

I'll sticky you the actual URL if it's any help....

Swanny007

6:58 am on Mar 5, 2008 (gmt 0)

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



While I'm on the topic... Jim can you adjust the code to only rewrite original web pages with the pattern z00001.php? In other words the first character will always be "Z" and there will always be 5 digits. If not then I don't actually need/want to rewrite. All the web pages follow that z00001 pattern. Thanks.

jdMorgan

7:16 am on Mar 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm using negative-match patterns, so that the pattern-matching engine doesn't scan all the way to the end of the requested URL, fail, and then have to back off one character at a time until it gets a match. Depending on the length of the URL-path tail, and whether or not a match is eventually found, using the negative match method can be hundreds of times faster. If multiple ".*" patterns are used, then changing to use negative-match patterns could easily be thousands of times faster. Unfortunately, in order to explain that any better, I'd have to refer you to the source code of the POSIX regular expressions library -- And even I'm not interested in trying to read that!

See the regular expressions tutorial cited in our charter [webmasterworld.com] for more information. Then have a go at "adjusting" that code yourself... Post back if you need more help.

Jim