RewriteRule with Multiple Variables not parsing correctly

Hello,

I’m having trouble with a Apache Rewrite rule that needs to pass two variables. It should be simple enough, but the output is not parsing the variable properly.

Here’s the rule as I’m using it:
RewriteRule ^/?(category)/([A-Za-z-]+)/([0-9-]+)/?$ /index.php?c=$2&start=$3 [L]

essentially, the input would be: http://www.example.com/category/dogs/2

which should internally be http://www.example.com/index.php?c=dogs&start=2

instead, what i get is http://www.example.com/index.php?c=dogs/2&start=

why are $2 and $3 not parsing?

Thanks in advance…

stn,

Welcome to SitePoint!

While I object to your mod_rewrite code on a number of technicalities, there is nothing to make it perform as you’ve indicated so it’s my guess that there is a typo in either what you’ve requested of your browser OR what you’ve presented above.

Regards,

DK

Yes, it was a typo. Lost a few hairs, but i figured it out.

This works:
RewriteRule ^/?cateogry/([A-Za-z-]+)?/([0-9-]+)/?$ /index.php?c=$1&start=$3 [L]

btw, why do you “object” to this code? security? speed?

First the ^/? at the start is used when you don’t know whether you are using Apache 1.x or Apache 2.x (1.x requires a / at the start, 2.x does not). Once you know, adjust your code accordingly.

Second, the ? at the end of ([A-Za-z-]+)? makes no sense at all, unless you really do have a category without a name. Drop that at the end there.

Third, the /? at the end is a bit weird. It’s up to you to determine what your URLs are, but now they work both with and without a trailing slash, which may lead to a duplicate content penalty from search engine spiders. I you must allow both with and without it’s better to 301 redirect one to the other.

Forth, the / in front of /index.php makes Apache look in the root of the filesystem first to see if the requested file is there. Only when that fails will it look at the root of your web directory. This slows things down unnecessarily, so remove the leading / there.

Lastly, $3 should be $2.

All in all, this would be the end result:


RewriteRule ^cateogry/([A-Za-z-]+)/([0-9-]+)$ index.php?c=$1&start=$2 [L]

Thanks for the suggestion - I’m using Apache2
I tried the rule with your changes, and yes it parses correctly when the category name is one word. However, if there’s a category with two or more names, with spaces, the browser forces a search.
e.g. category = Blue Room http://www.example.com/category/blue room

if I use the orgiinal rule with the ^/?category it works. With ^category it doesn’t.

I’m still a novice at apache rules and i see you can parse spaces with %20 but it doesn’t seem to work for me. any suggestions?
thanks again.

Quick update - i cleared my browser cache in Firefox, and then it worked. In Chrome it worked first time. Thanks again for the suggestions.

You can’t use spaces in URLs as you’ve found out, only %20, but that looks a bit stupid IMHO.
What most people do is replace spaces with dashes, so the URL would become /category/blue-room
The “blue-room” part of that URL is called a “slug”, which you normally store in the database (i.e. rewrite the category name to a slug when a category is created/updated) along with the record for Blue Room for easy lookup.

Rémon’s answer in post #5 was spot on! It only missed the confusion you’d be causing if you allow the trailing / because visitors COULD be at two levels in your file structure (resulting in relative links being unable to handle both cases). IMHO, trailing /'s should be reserved to the intended purpose (identifying a directory).

Rémon’s response in post #8 omitted my signature’s tutorial about replacing spaces with _'s when creating the links and changing back before querying the database. Yes, many people love -'s but that would eliminate the possibility of hyphenated words, wouldn’t it?

The bottom line is that my signature’s tutorial has been online for years, it’s been turned into a SitePoint article and it’s the basis for several sticky threads in this board. Take advantage of the information available (and ask the author directly if you have questions).

Regards,

DK

Yes, it would. But on the other hand, hyphens are better for search indexing, and I think most people consider that a more important factor.