Is there a better way than this???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
azycraze
Forum Commoner
Posts: 56
Joined: Mon Oct 24, 2011 12:08 pm
Location: India

Is there a better way than this???

Post by azycraze »


RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?module=$1&link=$2&control=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\=([a-zA-Z0-9_-]+)$ index.php?module=$1&link=$2&$3=$4
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?module=$1&link=$2
RewriteRule ^([A-Za-z0-9\_\-\/]+)/$ index.php?module=$1


is there any way to simplify this url rewriting???
pls help!!!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Is there a better way than this???

Post by Celauran »

Use a front controller. Redirect all requests to the front controller and let PHP sort out the rest. Much easier than trying to do it all through .htaccess.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Is there a better way than this???

Post by x_mutatis_mutandis_x »

Rewriting URLs in apache config for php scripts, is like <url-pattern></url-pattern> in web.xml for servlets. As long as you have a generic pattern matching with wild-characters, it would make more sense in using them. But for simple action routing, using front-controller pattern is better (like Celauren suggested) as you have a control over the routing scheme, and you can customize it to your needs.

Also avoid using .htaccess (esp. in production) as it's read on each time the directory under which it's located, is accessed by apache. If you have a virtual-host or default configuration, better to specify your rewrite rules in <Directory></Directory>. These files are read once when apache server starts/re-starts.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Is there a better way than this???

Post by pickle »

"a-zA-Z0-9_-" can be replaced with "\w-"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
azycraze
Forum Commoner
Posts: 56
Joined: Mon Oct 24, 2011 12:08 pm
Location: India

Re: Is there a better way than this???

Post by azycraze »

thanks for the replies..
can anyone suggest some links or pdf to understand more on front controller pattern for url rewriting?
Post Reply