Reescritura de mod de Apache

I have 2 rules set in .htaccess

RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ page.php?name=$1&do=$2&newstitle=$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ page.php?name=$1&do=$2&cattitle=$3 [L]

and this is how I call the url:

www.domain.com/My-Page/news/News-Title.html
www.domain.com/My-Page/category/Category-Title.html

and without mod rewrite:

www.domain.com/page.php?name=My-Page&do=news&newstitle=News-Title
www.domain.com/page.php?name=My-Page&do=category&cattitle=Category-Title

Now the problem is since both of the rules are similar, it always reverts to first rule, no matter what. If I remove the first rule, the second url works fine, but with both of them it always rewrites the first rule only.

How can I achieve this with both rules working. I know I'm missing something, but my expertise are limited when it comes to mod_rewrite.

Cualquier ayuda es muy apreciada.

preguntado el 30 de agosto de 11 a las 22:08

1 Respuestas

Both rules aren't similar, they're identical, so it's always going to do the first one.

If I were going to do this (and I really wouldn't, mostly because I feel this is a job better reserved for factories in an MVC pattern), I would probably say this:

RewriteRule ^([^/]*)/news/([^/]*)\.html$ page.php?name=$1&do=news&newstitle=$2 [L]
RewriteRule ^([^/]*)/category/([^/]*)\.html$ page.php?name=$1&do=category&cattitle=$2 [L]

You need some way to differentiate them. This should do that pretty simply.

Editar

Just to point out, you could write these:

RewriteRule ^(.+)/news/(.+)\.html$ page.php?name=$1&do=news&newstitle=$2 [L]

My added preference is to remove the .html:

RewriteRule ^(.+)/news/(.+)$ page.php?name=$1&do=news&newstitle=$2 [L]

I don't like exposing server technology in any sort, and the HTML adds nothing that we don't already know about the site.

Personally, I use a similar system:

RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

The URL is hacked apart by index.php, into a format like:

http://www.example.com/<controller>/<arg1>/<arg2>

This way, with one rewrite statement I'm telling it where to go, and the controller returns the appropriate output. I never have to update .htaccess after that. If I need new functionality, I write a new controller. Just something to look into.

Respondido 31 ago 11, 03:08

Thank you very much, now it all makes sense. - Mike

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.