Forum Moderators: phranque

Message Too Old, No Replies

Complex rewrite

confused by regex - how do I redirect these?

         

davidpbrown

7:49 pm on Aug 21, 2003 (gmt 0)

10+ Year Member



Could someone suggest how I could permanently redirect URL's
using htaccess and rewriterule to
1. replace all Capitals with lower case letters
2. and all _underscores with -hyphens?

I have tried searching for examples and even looked at learning regex but it appears daunting and more complex than the single file redirects I done before.

jdMorgan

11:10 pm on Aug 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



davidpbrown,

Using .htaccess limits the possibilities, since RewriteMap -- only available in a server config context -- might be a better solution.

However, two resources come to mind:

First, you might want to investigate Apache mod_speling [httpd.apache.org] to correct upper/lowercase issues.

Secondly, take a look at the RewriteRule [N] (Next) flag, which provides for re-running your RewriteRules if a match is found.

Although it's inefficient overall, I'd propose using a rule that replaces a single underscore with a hyphen, ending with an [N] flag. This will iteratively replace all hyphens. Put this code as lose as possible to the beginning of your Rules, because all of them will be re-processed for each underscore replaced.

On the last iteration, no underscore will be found, so the Rule will fall through. Follow this with a directive to enable mod_speling. mod_speling will then correct the case of the rewritten URI.

The above approach is untested, and (I believe) horribly inefficient. But if there are not other choices, such as using a script to process the URI, it might work.

HTH,
Jim

Storyteller

4:48 am on Aug 22, 2003 (gmt 0)

10+ Year Member



I used [N] rules for a somewhat similar task and the performance losses were absolutely impossible to notice.

However, getting mod_rewrite to interoperate with mod_spelling might not be straightforward. [PT] flag is the first thing to consider if you run into problems.

davidpbrown

8:31 am on Aug 22, 2003 (gmt 0)

10+ Year Member



I'm now looking at a php script to catch 404's..

so far I've got to
<?php
$str = $_SERVER['PHP_SELF'];
$str = strtolower($str);
$newstr = strtr($str, "_", "-");
if ($newstr==$str) {
header('Location: "error-404.pl');
} else {
header('Location: $newstr');
}
?>

but wonder before I go too far if there is disadvantage using php rather than Mod's? Would it be much slower to do this?

Thanks for the replys so far..

davidpbrown

12:03 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



Still need to know it's a good idea to use PHP rather than Mod's but continuing in PHP forum..
[webmasterworld.com...]