开发者

.htaccess redirect subdirectory as query string in php

This is hard to explain, so hopefully I'm understood in my question.

(1) I want to create "SEO friendly" links that remove the query string from a web site. There is only one variable, let's call it "page". Here is the following code for my .htaccess file.

Options +FollowSymLinks

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

This works in providing the proper redirect. So /applications/ will send to index.php?page=applications.

(2) My index.php will include a view page based on the value of $_GET['page']. Here is some sample code below:

switch ($_REQUEST['page']) {

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

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

}

There seems to be no problems so far.

(3) Let's make apps.php an exact copy of home.php. home.php loads just fine, but apps.php will not load linked CSS and JScript pages. When apps.php is loaded, it thinks it is in the /apps/ directory. To load the linked pages, I would need to insert a "../" in front of the file name. Then it displays correctly.

So my question is -- How can I properly write the .htaccess file so the home.php and apps.php page can be identical files and produce identical results, instead of the apps.ph开发者_StackOverflow社区p file being treated as if it were in the /apps/ directory?


First, I should apologize as I don't have a solution which involves making changes in the htaccess. My solutions are of a different nature.

I think the problem can be solved if you have a config variable,preferably in a config file, which will hold the root folder for images, js etc. Most of the time its public_html, the document root, where the url of your website points to. so your config variable could look like:

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

The config file should be included in index.php unconditionally.

So, when you include any js or images, you do it like this:

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

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

If you include the config file in index.php, all the files you include based on switch-case conditions, will be able to use the $base_url variable.

Another possible solution is to use the base tag. Look it up here:

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

I hope this helps.


use absolute urls for js, css and images on your pages (starting with a slash).

/js/main.js instead of js/main.js


You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). But then you expose the query string, which is what you wanted to avoid in the first place.

The reason it can't be done: It is not apps.php which "thinks it is in the /apps/ directory" - it's the browser which "thinks" that. In the page source generated by apps.php, you send relative URLs back to the browser, and now the browser will request these resources relative to the location of the page it asked for. For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side.

So the options you have are:

  • Do an external redirect with your .htaccess (and defeat your original purpose ;-)
  • Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs)
  • Use absolute URLs, just as @nobody has suggested in his answer.

The last one is the only real option IMHO.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜