Jump to content

Trouble using .htaccess to simulate website directories (using GET parameters)


stickynote427

Recommended Posts

I recently redid my website, and before I started, I decided that I would use GET variables to decide what page would be loaded.

 

For example, http://www.example.com/?p=home with PHP something like this:

 

<?php

include($_GET['home'].".php");

?>

 

Would load the home.php file.

 

I then used .htaccess to make the URL look nicer, and http://www.example.com/p/home forwards to the same URL.

 

I have that all working fine. Now I'm trying to make a web applications section of my site (http://www.example.com/p/webapps). Here's what I would like to have happen:

  • If URL is http://www.example.com/p/webapps or .../p/webapps/ (notice the forward slash at the end), then a basic "Here are the web apps" page is shown.
  • If URL is .../p/webapps/app and web application "app" is set up and exists, then a "This is the App web app" page is shown.
  • If URL is .../p/webapps/app and web application "app" is not set up and/or doesn't exist, then the basic "Here are the web apps" page is shown.

 

I mention the URL with the forward slash at the end in #1, because I am having trouble writing my .htaccess file and web applications page so that everything works how I want it above. Whenever I have .../p/webapps or .../p/webapps/ with the ending slash, all kinds of crazy stuff happens: depending on the order of the items in the .htaccess files, as well as how I've written my web applications page, I might get PHP include errors, or nothing shows up at all. If I then rewrite my web applications page, I might be able to get everything with those URLs working, but say if I go to .../p/webapps/app and I don't have application "app" set up yet or it doesn't exist, I still don't see the basic welcome page.

 

So now, here is how my site is set up:

 

Regular HTML stuff is loaded (html, head tags, etc.)

  • http://www.example.com is my home page, which is index.php.
  • If there is no /p after the URL, then the default "home" page is included on the page.
  • If there is a /p after the URL, but the page after that does not exist (such as /p/foo), the "home" page is included.
  • If there is a /p after the URL and the page after that DOES exist, then that page is included.

 

In the case that the URL is .../p/webapps, the "webapps" page is included, as it should be.

 

  • Checks if there is a webapp specified (.../p/webapps/app)
  • If there is, then it is included on the page.
  • If there is not, I try to show the default page. This is about where I run in to trouble.

 

Here is my .htaccess file that makes .../p/about go to ?p=about, etc.

 

RewriteRule ^p/$ / [QSA]
RewriteRule ^p$ / [QSA]

RewriteRule ^p/webapps/$ /p/webapps [QSA]

RewriteRule ^p/webapps/(.+)$ ?p=webapps&app=$1 [QSA]
RewriteRule ^p/(.+)$ ?p=$1 [QSA]

 

That's about all I can think of posting here. I know this is a lot, and it is probably difficult to follow without having my filesystem to look at and more source code. Please, please tell me if you need me to simply some things, you need more of the PHP code I have right now, etc.

 

Thanks.

stickynote427

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.