Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite rule request

Want a rewrite rule written

         

krt1

11:30 am on May 15, 2005 (gmt 0)

10+ Year Member



I want a rewrite rule that will do the following "Rewrites"

/asp -> /index.php?c=asp&s=&p=
/php/security -> /index.php?c=php&s=security&p=
/seo/google/3 -> /index.php?c=seo&s=google&p=3

I just want the basic rule, no flags etc.
My unsuccessful attempt:
RewriteRule ^([^/]*)/*([^/]*)/*([^/]*)* index.php?c=$1&s=$2&p=$3 [NC,L]

Thanks,
krt

jdMorgan

2:56 pm on May 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



krt,

Welcome to WebmasterWorld!

Addressing this thread's title, we're here to discuss Apache, and not to write code on demand -- Please review our forum charter [webmasterworld.com].

That said, there are a few questions that need to be answered in the interest of server efficiency. The main one being whether these are the only three requested URL forms that you want to rewrite. If so, then using three separate, specific rules would be the best way to go.

If you have other forms of URLs that you also need to rewrite, then those have to be taken into account. The goal is to balance the processing time of specific rules against that of a generic ambiguous rule. Bearing in mind that an ambiguous rule takes longer to process -- sometimes much longer, and that every single request to your server will be processed through it, it's often better to use specific rules -- up to the point where you have several dozens of rules.

Specific:

 RewriteRule ^asp/?$ /index.php?c=asp&s=&p= [L]
RewriteRule ^php/security/?$ /index.php?c=php&s=security&p= [L]
RewriteRule ^seo/google/3/?$ /index.php?c=seo&s=google&p=3 [L]

Semi-generic:
 RewriteRule (asp)/?¦(php)/(security)/?¦(seo)/(google)/(3)/?$ /index.php?c=$1&s=$2&p=$3 [L]

More generic:
 RewriteCond %{REQUEST_URI} !^index\.php$ 
RewriteRule ^([^/]+)(/([^/]+)(/([^/]+))?)?/?$ /index.php?c=$1&s=$3&p=$5 [L]

There are many other possible solutions in addition to these. Which is best depends on the nature and number of URLs that you want to rewrite, and the nature and number of URLs that you don't want to rewrite.

Notes:

  • Replace all broken pipe "¦" characters with solid pipe characters before use.
  • To resolve nested back-reference assignments, count left parentheses.
  • I added the "/?" subpatterns at the end to allow for a trailing slash, which may be added by some user-agents.
  • The RewriteCond on the last rule is needed to prevent a rewrite loop, since "index.php" would otherwise match the rule pattern and get rewritten repeatedly until the client or server timed out.

    Jim

  • krt1

    5:38 am on May 16, 2005 (gmt 0)

    10+ Year Member



    I did read the forum charter however I was hoping this would sneak in on the right side of the border line because
    1) I already tried to do it myself
    2) I only wanted one or two lines of code.

    Thankyou for your informative reply!