punk_runner Posted January 4, 2011 Share Posted January 4, 2011 I am using Apache's mod_rewrite to create nice looking URL's with this pattern: www.domain.com/controller/method/arg1/arg2/ using this: RewriteRule ^([a-zA-Z]*)/([a-zA-Z]*)/([a-zA-Z]*)/([a-zA-Z]*)/$ index.php?c=$1&m=$2&a1=$3&a2=$4 [L] I want to be able to submit the URL to the index (front controller) even if all of the variables are empty, so I can error check, but for example, when I enter www.domain.com/user into my browser, I get a 404 error... I want to parse the URL at the index and throw an exception for "No Method Found"... How do I set my regex so that it accepts empty values for any of the sections of my url, so that these all resolve to index.php? www.domain.com/user www.domain.com/user/view www.domain.com/user/view/bobsmith www.domain.com/user/view/bobsmith/friends Quote Link to comment https://forums.phpfreaks.com/topic/223418-mod_rewrite-question/ Share on other sites More sharing options...
johnny86 Posted January 5, 2011 Share Posted January 5, 2011 You should just add three more rewrite rules: RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?c=$1&m=$2&a1=$3&a2=$4 [L] RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?c=$1&m=$2&a1=$3 [L] RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?c=$1&m=$2 [L] RewriteRule ^([a-zA-Z]+)/?$ index.php?c=$1 [L] I modified your regex so that it will require atleast one character after every /. I also added ? at the end of the regex to make / optional at the end of the url. Your regex would not match www.domain.com/controller/method/arg1/arg2 because you have put / staticly there. In that order, you have your routes to check if the most args are set and fall back down as the arguments are missing. Quote Link to comment https://forums.phpfreaks.com/topic/223418-mod_rewrite-question/#findComment-1154963 Share on other sites More sharing options...
punk_runner Posted January 5, 2011 Author Share Posted January 5, 2011 Okay, but in an effort to make this extensible, and assuming that the URL could present no bits, like just domain.com or eight bits like domain.com/controller/method/arg1/arg2/arg3/arg4/arg5/arg6/, is there a cleaner way that mod_rewrite can say apply [a-zA-Z]+) to as many URL bits as it needs to? Then I could explode the URL in the front_controller and set the controller and method values to create a new object,and roll the remaining args into an array to be passed to the method, where the controller will decide what to do with them. There has to be a simpler way that you can apply across an entire site and any length of URLs right? Quote Link to comment https://forums.phpfreaks.com/topic/223418-mod_rewrite-question/#findComment-1155063 Share on other sites More sharing options...
johnny86 Posted January 5, 2011 Share Posted January 5, 2011 Well in that case you need to set your routes up in the front controller and direct all requests to your frontcontroller. I tought your idea was to route your application from apache to the right place in the first place. I've been using this: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L] That will simply forward all requests to index.php. Then you can extract the args from your $_SERVER['REQUEST_URI'] in your php script Quote Link to comment https://forums.phpfreaks.com/topic/223418-mod_rewrite-question/#findComment-1155147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.