matcartmill
matcartmill

Reputation: 1593

Mod_Rewrite to clean URLs

I'm trying to create a WordPress style URL system where I can have the user create a page, which automatically creates a "slug" version of it.

I will be using index.php to query the database to match the slug to the page.

I have tried copying parts of WordPress' .htaccess, however shows a 404 error.

My URL format is similar to the following:

www.mysite.com/about
www.mysite.com/rentals/page1

My .htaccess file looks like:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Once I get the htaccess part solved, how do I the slug from the URL? I imagine that even though the URL would be clean it still might possible to send it through as index.php?page=[SLUG]

Any ideas on where to start and how to accomplish what I'm looking for?

Upvotes: 0

Views: 49

Answers (1)

Brian
Brian

Reputation: 1025

I'm using the following...

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

I use $_SERVER['REDIRECT_URL']to get the path/page-name.

example... index.php/about

$request = strip_tags(trim($_SERVER['REDIRECT_URL']));
$request = rtrim($request, "/"); // remove trailing slash 
$pg = explode('/', $request);
$page = $pg['1'];
// SELECT FROM table WHERE page = $page;

Upvotes: 1

Related Questions