DominicM
DominicM

Reputation: 6908

Can't get mod_rewrite to work

I am trying to get url from:

192.168.0.1/movie-page.php?id=123

to:

192.168.0.1/movie/movie-name

or even (for now):

192.168.0.1/movie/123

I've simplified it by using this url (to get something working):

192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/

my .htaccess file:

RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]

What am I doing wrong? I've tried many combinations but no luck and my patience is running out...

I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.

Upvotes: 0

Views: 156

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

If you want to rewrite: 192.168.0.1/movie-page.php?id=123 too

192.168.0.1/movie/movie-name or 192.168.0.1/movie/123

Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]

So a better method is to route everything through the rewrite:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

Then handle the route by splitting the $_GET['route'] with explode()

<?php 
//192.168.0.1/movie/movie-name
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);

if(!empty($route)){
    //example
    $route[0]; //movie
    $route[1]; //movie-name
}
?>

Upvotes: 2

davidethell
davidethell

Reputation: 12018

You want something like this:

RewriteRule ^movie/([0-9]*)$ /movie-page.php?id=$1 [L,R=301]

That will give the movie ID version with a numeric ID.

Upvotes: 0

Related Questions