-1

I have a database with movies, each video has an ID, TITLE and YEAR. I would like to for everyone 'Friendly URLs'. I know I have to use mod_rewrite, How to do it dynamically?

Recently I did it by appending to a file something like RewriteRule movie-title-year movie.php?id=17 at the end of the file - when I added the movie through the form.

But that is not a good way out.

I would like it to be somehow generated automatically like /movie-title-year for each movie.

Sorry for my English.

Šime Tokić
  • 650
  • 1
  • 9
  • 20
  • 5
    Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Croises Nov 04 '17 at 23:17
  • You actually could do this without mod rewrite, you could do it by saving a slug or hash in the DB and then when you look up movie in the DB use that instead of the id, you could do the link like `mydomain.com/movie.php/{slug}` and then if you really wanted remove the `movie.php` with rewriting.you could do that. For future movies you just put a slug in when you first create them ... no rewrite changes. – ArtisticPhoenix Nov 04 '17 at 23:23
  • I am way to lazy to muck with rewrite rules these days. I have like 4 lines in there, and I have pretty urls, they are the prettiest in fact. – ArtisticPhoenix Nov 04 '17 at 23:28

1 Answers1

0

How I would do it without, or with minimum use of mod_rewrite.

It will take some work, hard to say without knowing more about your setup.

First off I would change the schema in the DB.

  1. Backup the table ( call it movies as i don't know what you call it) - always make a backup.

  2. Output the table schema: SHOW CREATE TABLE movies

  3. Modify the table schema add our new column slug add a unique index for the column change table name to 'tmp_movies'

  4. Write PHP script to copy the records from old table to new table patch missing slug (maybe the title lowercase with - in place of spaces) would have to account for possible duplicate slugs.

  5. DROP the orignal table, and rename the tmp table to movie

Now your DB is fixed.

  1. Change how you look up the pages, from using an id etc. to using the slug.

  2. Change how you add new movies to create a slug.

  3. Add one simple rewrite rule to replace the new way you look up movies.

Something like ( i'm just guessing at the htaccess)

   //type1  (URI)
   www.example.com/index.php/movies/slug  //typical url

   ^movies/([^/]*) index.php/movies/$1 [L]  //htaccess

   www.example.com/movies/slug  //re-written url

   -------------------------------------------------------------
   //type2  (Query)
   www.example.com/index.php?page=movies&slug=slug  //typical url

   ^movies/([^/]*) /index.php?page=movies&slug=$1 [L] //htaccess

   www.example.com/movies/slug  //re-written url

See your issue right now is you probably don't have much more then the ID in your url. Like:

   www.example.com/movies/index.php?movie_id=2034

You need something in there with more appeal like this

   www.example.com/movies/murder-on-the-orient-express 

And the only way to go from this movie_id=2034 to murder-on-the-orient-express is to probably rewrite each one manually ( which is ridiculous )or add that something you are missing.

Hope that helps.

ArtisticPhoenix
  • 20,683
  • 2
  • 18
  • 34