This really is difficult to explain, so hopefully I am understood during my question.

(1) I wish to create "Search engine optimization friendly" links that take away the query string from an internet site. There's just one variable, let us refer to it as "page". This is actually the following code for my .htaccess file.

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1

This works in supplying the correct redirect. So /applications/ will be sending to index.php?page=applications.

(2) My index.php includes a view page in line with the worth of $_GET['page']. Here's some sample code below:

switch ($_REQUEST['page']) {

    default:
    include ("home.php");
    break;

    case "apps":
    include ("apps.php");
    break;  

}

There appears to become no problems to date.

(3) Let us make apps.php a precise copy of home.php. home.php loads all right, but apps.php won't load linked CSS and JScript pages. When apps.php is loaded, it thinks it is incorporated in the /apps/ directory. To load the linked pages, I will have to place a "../" while watching file title. It shows properly.

So my real question is -- How do i correctly write the .htaccess file therefore the home.php and apps.php page could be identical files and convey identical results, rather than the apps.php file receiving treatment as though it were within the /apps/ directory?

First, I ought to apologize when i do not have an answer that involves making alterations in the htaccess. My solutions are of the different character.

I believe the issue could be solved for those who have a config variable,ideally inside a config file, that will contain the root folder for images, js etc. More often than not its public_html, the document root, in which the website indicates. so that your config variable could seem like:

$base_url = 'http://www.mywebsite.com/';

The config file ought to be incorporated in index.php without any reason.

So, whenever you include any js or images, you're doing so such as this:

<script type="text/javascript" src="<?php echo $base_url;?>js/global.js" />

<img src="<?php echo $base_url;?>images/gradient_green.jpg" />

Should you range from the config file in index.php, all of the files you include according to switch-situation conditions, will have the ability to make use of the $base_url variable.

Another possible solution is by using the bottom tag. Look up here:

http://www.w3schools.com/TAGS/tag_base.asp

I really hope this can help.

use absolute web addresses for js, css and pictures in your pages (beginning having a slash).

/js/primary.js rather than js/primary.js

You cannot do this with .htaccess unless of course you need to do an exterior redirect (with the addition of the [R] flag for your RewriteRule). However you expose the query string, that is that which you desired to avoid to begin with.

The main reason it cannot be achieved: It's not applications.php which "thinks it is incorporated in the /applications/ directory" - it is the browser which "thinks" that. Within the page source produced by applications.php, you signal relative Web addresses to the browser, and today the browser will request these assets in accordance with the position of the page it requested. For that browser, the page it got is within /applications/, regardless of what spinning you applied internally around the server side.

Therefore the options you've are:

  • Do an exterior redirect together with your .htaccess (and defeat your original purpose -)
  • Alter the Web addresses dynamically with PHP while processing applications.php etc, while you stated (prefixing ../ towards the Web addresses)
  • Use absolute Web addresses, just like @nobody has recommended in the answer.

The final the first is really the only option IMHO.