Crazy Horse
asked on
mod_rewrite with MVC
I am creating a small site and using procedural PHP for my controller:
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
if(isset($_GET['page']) == 'about-us') {
include("views/about-us.php");
}
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]
ASKER
Thanks, Dave. But if I don't use isset then I keep getting an annoying undefined index error on my home page.
Also, removing isset did not change anything.
include("views/header.php");
if($_GET['page'] == 'about-us') {
include("views/about-us.php");
} else {
include("views/home.php");
}
include("views/footer.php");
Also, removing isset did not change anything.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think I figured it out.
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 [NC,L]
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
I honestly don't know.
ASKER
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
?
I see no problem. That's exactly what I'm doing on a couple of sites.
ASKER
Thank you!
http://php.net/manual/en/function.isset.php