Search code examples
phpmysql.htaccesspermalinks

Permalinks Not Working with MySQL Database


I have a simple MySQL database with 3 fields (ref_id, slug, content). I have it populated with the following data (1, about, Example content; 2, portfolio, Example content; 3, contact, Example content).

I created a basic PHP page with the following code:

<?php
require('db.php');

$ref_id = $_GET['ref_id'];

$query = "SELECT * FROM myWebsite WHERE ref_id='$ref_id'";
$page_query = mysql_query($query);

 mysql_close();

$slug = mysql_result($page_query, 0, "slug");
$content = mysql_result($page_query, 0, "content");

echo "<h1>$slug</h1>
<p>$content</p>";
?>

When you go to mydomainname.com/myWebsite.php?ref_id=2 you would see the Portfolio page, etc.

Everything works up until here.

Instead of using mydomainname.com/myWebsite.php?ref_id=2 I want to use mydomainname.com/portfolio

Based on the tutorials I found and what I know I created a .htaccess file and added the following code:

<IfModule mod_rewrite.c>
RewriteEngine on
# Change following path
RewriteBase /$slug/
RewriteRule ^([A-Za-z0-9-]+)/?$ myWebsite.php?ref_id=$1 [L]
</IfModule>

This does not work --- when I go to mydomainname.com/portfolio I get "The requested URL /$slug/myWebsite.php was not found on this server." What am I missing here?


Solution

  • I think you copied the following verbatim from those tutorials:

    RewriteBase /$slug/
    

    Remove the above line and restart server.