Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

mod_rewrite with MVC

I am creating a small site and using procedural PHP for my controller:

if(isset($_GET['page']) == 'about-us') {

    include("views/about-us.php");

}

Open in new window


My URL then looks like:

http://example.com/?page=about-us

I just want that to read:

http://example.com/about-us

but I am not sure how to do this. When not using this approach, this worked for such simple tasks but not in this instance.

I tried something like this which would have .html on the end but would look like a better url .This however doesn't seem to work

RewriteEngine On
RewriteRule ^([^-]*)\.html$ /?page=$1 [L]

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Your code is not valid.  'isset' only returns true or false/null and can't be compared to a string.

http://php.net/manual/en/function.isset.php
Avatar of Crazy Horse

ASKER

Thanks, Dave. But if I don't use isset then I keep getting an annoying undefined index error on my home page.

include("views/header.php");

if($_GET['page'] == 'about-us') {
	
	include("views/about-us.php");
	
} else {

include("views/home.php");

}
include("views/footer.php");

Open in new window


Also, removing isset did not change anything.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think I figured it out.

RewriteRule ^about-us ?page=about-us [NC,L]

Open in new window


If I navigate to

mysite.com/about-us

it takes me to the correct url i.e.: mysite.com/?page=about-us

But, how can I make this more generic? I wouldn't want to have to do this for every page I have, like :


RewriteRule ^about-us ?page=about-us
RewriteRule ^contact-us ?page=contact-us
RewriteRule ^services ?page=services

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, Dave. I didn't have a problem without the $ but then I am running on localhost so maybe it's different? Anyway, I will add the $ for correctness.

Okay, so it is okay to have like you say, pretty much as many lines as you need. So, there is no way to kind of "catch all" with just one line?

For example, I used to use this to make any standard php pages remove the .php without having to do it for every page.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

Open in new window

I honestly don't know.
Okay, but you see nothing wrong with doing it as you suggested for as many pages as required like this:

RewriteRule ^about-us$ ?page=about-us
RewriteRule ^contact-us$ ?page=contact-us
RewriteRule ^services$ ?page=services

Open in new window


?
I see no problem.  That's exactly what I'm doing on a couple of sites.
Thank  you!