3

Say im using .htaccess file for clean looking links, but that I have a long list (say 20) of RewriteRules to do things like this

domain.com/user/adam --> domain.com/user.php?need=user&user=adam
domain.com/test.js --> domain.com/index.php?need=code&file=test.js
domain.com/test.js/edit --> domain.com/index.php?need=code&file=test.js&action=edit

Is it better to use a catch all RewriteRule and split on the slash/ parse and direct in PHP? Or is the long list of RewriteRule better?

Better I guess I mean faster, and more robust. I know that upkeep would be better with the catch all.

Also, can you recommend a php class example/article on doing this?

Adam Meyer
  • 1,505
  • 1
  • 19
  • 28
  • 1
    Are u using any frameworks? Using MVC? – Raj Jan 28 '12 at 17:13
  • This is very close to my own question ;) Here : http://stackoverflow.com/questions/7702667/php-handling-vs-apache-rewriterules-and-regexp-which-one-is-quicker – Olivier Pons Jan 28 '12 at 17:53
  • It is being implemented using MVC methods, but im not using a framework. – Adam Meyer Jan 28 '12 at 19:14
  • 1
    Whatever works for you is fine. If you find yourself having to change/add rules frequently it might be better to find a more generic solution, like a catch-all. 20 rules is not a lot, when you are talking about performance. I would not worry about that. – Gerben Jan 29 '12 at 11:48

2 Answers2

1

If this answers what your asking, there is no problem using a massive list of rewrite rules. I have seen rewrite rules that fill my whole monitor and then some - there is no problem with a massive list unless you have a problem with them.

Shane
  • 2,007
  • 18
  • 33
0

You might want to look at ZendRouter - even though you're not using a framework, you may want to follow their ideas.

http://framework.zend.com/manual/en/zend.controller.router.html

Essentially .htaccess routes all requests to the index.php file with the exception of any matches to the RegEx:

RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php

Or any non files / directories (though I think there is a slightly higher cost to check if a file/directory exists rather than regex the requested URL)

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Once you've leveraged this type of routing - you can define a standard regex for URLs to map to controllers/actions

cloakedninjas
  • 4,007
  • 2
  • 31
  • 45