Clean URL with php and a little mod rewrite

New to mod_rewrite? This is a good place to start.

Clean URL with php and a little mod rewrite

Postby vinsud » Tue Sep 22, 2009 8:51 am

Hello, I'm new here and I have no experience with with mod rewrite / clean url.

I want to clean my urls with php instead of mod rewriting. I only need mod rewrite to redirect all request to index.php.


index.php: (part of it)

$url = strip_tags($_SERVER['REQUEST_URI']);
$url_array = explode("/",$url);
array_shift($url_array);
if (!empty($url_array[0])) {
if (file_exists('pages/'.$url_array[0].'.php')) {
include('pages/'.$url_array[0].'.php'); }
else {
include('pages/error404.php'); }
}
else {
include('pages/home.php'); }


.htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


Links on the homepage (index.php) are working.
Example, <a href="example">Example</a> redirect to "http://www.mysite.com/example" and show the correct page.

On this page (http://www.mysite.com/example) links such as <a href="example/test">Test</a> are not working properly.
The url in the browser is correct (http://www.mysite.com/example/test), but no images, js and css are loaded.
A link on this page (http://www.mysite.com/example/test) like <a href="home">Home</a> isn't working either and redirect to
"http://www.mysite.com/example/home" instead "http://www.mysite.com/home"

In map 'pages' the file "example.php" exist. File "test.php" does not exist and is virtual.

I cannot figure it out?
vinsud
 
Posts: 2
Joined: Tue Sep 22, 2009 8:47 am

Postby richardk » Tue Sep 22, 2009 9:36 am

richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby vinsud » Wed Sep 23, 2009 12:22 am

Thank You Richard.

It works!
vinsud
 
Posts: 2
Joined: Tue Sep 22, 2009 8:47 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 26 guests

cron