skip to Main Content

So I’ve been searching for a while now and can’t find anything specific on how to create a pretty url / seo / slug url type system WITHOUT sending everything to a index.php or moving things into subfolders.

Basically I’m making a website which you can currently go to urls like movie.php?id=#### / show.php?id=####. Ideally I’d like the url to be movie/#### or movie/id/#### (or down the line slugs of the name that i can use to grab the right one) etc.

Is there a way to do it without having a single index.php router or am I just going to have to rewrite all my files to adhere to this style?

2

Answers


  1. Yes, assuming your URL structure follows a relatively consistent pattern, you can definitely do this with an .htaccess file and mod_rewrite (without the need for an index.php file, commonly referred to as a front controller).

    Here’s a really simple example:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    
    RewriteRule ^(.*)/(.*)$ $1.php?id=$2 [NC,L]
    

    This takes an incoming URL like http://example.com/movie/3 and performs a transparent/internal rewrite (so the user doesn’t see the URL change) to http://example.com/movie.php?id=3.

    Of course, this example could be expanded to handle more parameters, etc. but hopefully this gets you started on the right path. I highly recommend you read the mod_rewrite documentation for more details: http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

    Login or Signup to reply.
  2. You can create a rewrite rule in .htaccess that routes the movie urls to movie.php as follows:

    movie/123:

    RewriteRule ^movie/(d+)$ movie.php?id=$1 [L]
    

    movie/id/123:

    RewriteRule ^movie/id/(d+)$ movie.php?id=$1 [L]
    

    movie/title-of-movie:

    RewriteRule ^movie/(S+)$ movie.php?slug=$1 [L]
    

    movie/title/title-of-movie:

    RewriteRule ^movie/title/(S+)$ movie.php?slug=$1 [L]
    

    combination movie/123/title-of-movie:

    RewriteRule ^movie/(d+)/(S+)$ movie.php?id=$1&slug=$2 [L]
    

    Edit: added a full .htaccess example for 1 required with up to 2 extra optional parameters with a fallback on index.php if the url is not for movies.

    Options -Indexes
    IndexIgnore */*
    
    Options FollowSymLinks
    AddDefaultCharset utf-8
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        RewriteRule ^movie/([^/]+)/?([^/]*)/?([^/]*)$ movie.php?param1=$1&param2=$2&param3=$3 [L]
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule . index.php [L]
    </IfModule>
    

    ^ to match from the beginning
    $ to match until the end
    ? for 0 or 1 occurrence
    + for 1 or more occurrences
    * for 0 or more occurrences

    If the url rule does not match and the file does not exist then it will route the url to index.php, but you can remove that last part if you don’t want that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search