Forum Moderators: phranque

Message Too Old, No Replies

modrewrite question

         

digital fury

2:26 am on Nov 24, 2008 (gmt 0)

10+ Year Member



Hello. I currently use a mod-rewrite rule to redirect traffic through my index page and it works fine:

RewriteRule ^([A-Za-z]*)([-]*)([A-Za-z]*)([-]*)([A-Za-z]*)$ /index.php?page=$1$2$3$4$5 [NC,L]

One page has two hyphens in the url, so thats why the two ([-]*).

I was looking online for a way to simplify these two, so i came up with:

RewriteRule ^(.*)$ /index.php?page=$1 [NC,L]

but this rule causes an apache server error 500. I don't get it. It looks fine to me. Can someone help me out here? Thanks.

phranque

8:40 am on Nov 24, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], digital fury!

whenever you see an apache server error 500 it's a good idea to look in the server error log for clues.

g1smd

8:50 pm on Nov 24, 2008 (gmt 0)

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



Firstly it redirects to itself in an infinite loop.

Secondly it also redirects for robots.txt and other such files. That should not happen.

.

Why simplify the rule? In many cases that makes it less efficient.

Make the rule fit exactly which URLs need to be matched, and no more.

Additionally, if a part of a URL must always be present, change the * to a + where necessary.

For example, the very first group should have a + if there are no URLs that begin with a hyphen.

If you have NC at the end, then you don't need to specify both A-Z and a-z, as the NC already says "use any case".

Do any URL parts use digits? There is no provision for that in the rules.

jdMorgan

7:58 pm on Nov 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's one way to simplify that:

RewriteRule ^(([a-z]+-)+)[a-z]+)$ /index.php?page=$1 [NC,L]

The pattern says, "Match one or occurrences of (one or more uppercase or lowercase alphabetic characters followed by a hyphen), followed by one or more alphabetic characters." This handles both your single-hyphen and double-hyphen case, as well as cases where you have more than two hyphens.

If you wish, you can restrict it to match URL-paths containing only one or two character-hyphen sequences:


RewriteRule ^(([a-z]+-)[b]{1,2}[/b])[a-z]+)$ /index.php?page=$1 [NC,L]

This is mainly a regular-expressions pattern-matching problem, not specifically a mod_rewrite problem. So the best solution depends on exactly what "classes" of URLs you want to match, with those classes defined by the regular-expressions pattern.

Jim