Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/223418-mod_rewrite-question/
Share on other sites

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.

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.