Messed up in url-rewriting

Viewed 107

So, thinking and creating a new site, video gallery style, with categories, models, videos, etc.

I created scripts, pages, urls, etc., now it would be nice to create an url_rewriter, to simplify and make urls more SEO.

But I got messed up and I'm getting confused more and more.

These are the urls of my site, and the urls in which I would like to transform them.

index.php
my-url.com

categories.php
my-url.com/categories/

category.php?category=category
my-url.com/category/

category.php?category=category&page=1
my-url.com/category/page-1/

category.php?category=category&sort=sort
my-url.com/category/sort/

category.php?category=category&page=1&sort=sort
my-url.com/category/page-1/sort/

my-url.com/models.php
my-url.com/models/

my-url.com/model.php?model=Model-Name
my-url.com/model/Model-Name

my-url.com/model.php?model=Model-Name&page=1
my-url.com/model/Model-Name/page-1/

my-url.com/model.php?model=Model-Name&sort=sort
my-url.com/model/Model-Name/sort/

my-url.com/model.php?model=Model-Name&page=1&sort=sort
my-url.com/model/Model-Name/page-1/sort/

my-url.com/videos.php
my-url.com/videos/

my-url.com/videos.php?page=1
my-url.com/videos/page-1/

my-url.com/videos.php?sort=sort
my-url.com/videos/sort/

my-url.com/videos.php?page=1&sort=sort
my-url.com/videos/page-1/sort/

my-url.com/video.php?title=video-title
my-url.com/video/video-title.html

This is the url rewrititng that I have written so far and that now does what it wants, and not what it should do or I would like it to do.

RewriteRule ^([^/]+)\/$ category.php?category=$1 [NC,L]
RewriteRule ^([^/]+)/page-([^/]+)/$ category.php?category=$1&page=$2 [NC,L]

RewriteRule ^([^/]+)\/([^/]+)\/$ category.php?category=$1&sort=$2 [NC,L]
RewriteRule ^([^/]+)/page-([^/]+)/([^/]+)/$ category.php?category=$1&page=$2&sort=$3 [NC,L]


RewriteRule ^models/$ models.php

RewriteRule ^model/([[A-Za-z0-9_]]+)\/$ model.php?model=$1 [NC,L]
RewriteRule ^model/([^/]+)/page-([^/]+)\/$ model.php?model=$1&page=$2 [NC,L]

RewriteRule ^model/([^/]+)\/([^/]+)\/$ model.php?model=$1&sort=$2 [NC,L]
RewriteRule ^model/([^/]+)/page-([^/]+)/([^/]+)\/$ model.php?model=$1&page=$2&sort=$3 [NC,L]


RewriteRule ^videos/$ page.php
RewriteRule ^videos/page-([^/]+)\.html$ page.php?page=$1 [NC,L]


RewriteRule ^video/([^/]+)\.html$ out.php?id_photo=$1 [NC,L]

By now I think I really messed up, and the more I touch it the worse it seems to go... even if I go to the url my-site.com/model/Model-Name, he understands that he must use the model.php file, but not sees the model-name variable, and I really don't understand why anymore.

So, I need some help to understand how to write the url_rewriter.

And I'd also like to know if this is the best directory/url structure for a site like this or if there are better ones.

Any suggestions or help will be truly appreciated (because I'm really confused)

1 Answers

I found some mistakes in your rulesets, I have edited them below and summed up the changes:

RewriteRule ^models\/$ models.php [NC,L]

RewriteRule ^model/([A-Za-z0-9_]+)\/$ model.php?model=$1 [NC,L]
RewriteRule ^model/([A-Za-z0-9_]+)\/page-([^/]+)\/$ model.php?model=$1&page=$2 [NC,L]

RewriteRule ^model/([A-Za-z0-9_]+)\/([^/]+)\/$ model.php?model=$1&sort=$2 [NC,L]
RewriteRule ^model/([A-Za-z0-9_]+)/page-([^/]+)/([^/]+)\/$ model.php?model=$1&page=$2&sort=$3 [NC,L]


RewriteRule ^videos\/$ page.php [NC,L]
RewriteRule ^videos\/page-([^/]+)\.html$ page.php?page=$1 [NC,L]


RewriteRule ^video/([^/]+)\.html$ index.php?id_photo=$1 [NC,L]

RewriteRule ^([^/]+)\/$ category.php?category=$1 [NC,L]
RewriteRule ^([^/]+)/page-([^/]+)/$ category.php?category=$1&page=$2 [NC,L]

RewriteRule ^([^/]+)\/([^/]+)\/$ category.php?category=$1&sort=$2 [NC,L]
RewriteRule ^([^/]+)/page-([^/]+)/([^/]+)/$ category.php?category=$1&page=$2&sort=$3 [NC,L]
  • The rules will be executed top down, and therefor the model rule will never be executed because the first ruleset you use already matches any request: model will become a category and the model-name will become a sorting. To resolve this, you have to start with the rule that is the most restrictive and end with the rule that will be the least restrictive. When a request then does not start with model(s) or video(s), it will at last try to rewrite it in category.

  • In the first model/ rewrite rule you use double brackets [, you should use one in this case.

  • In the videos rule, you forgot to declare the flags necessary
  • I notice that you only allow alphanumerical characters and the underscore in the first rule, but allow all characters except for the forward slash in the second rule, I have taken the liberty to change this.

Also notice that you need a trailing slash at each request with your rules before the request will match any of the above. If you want to have the trailing slash optional, you can replace the / before the $ with (\/|)

Related