简体   繁体   English

.htaccess重定向子目录作为php中的查询字符串

[英].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. (1)我想创建“ SEO友好”链接,以从网站中删除查询字符串。 There is only one variable, let's call it "page". 只有一个变量,我们称它为“页面”。 Here is the following code for my .htaccess file. 这是我的.htaccess文件的以下代码。

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 . 因此, /applications/将发送到index.php?page=applications

(2) My index.php will include a view page based on the value of $_GET['page'] . (2)我的index.php将包含一个基于$_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 . (3)让我们apps.php的精确副本home.php home.php loads just fine, but apps.php will not load linked CSS and JScript pages. home.php可以很好地加载,但是apps.php不会加载链接的CSS和JScript页面。 When apps.php is loaded, it thinks it is in the /apps/ directory. 加载apps.php ,它认为它位于/apps/目录中。 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.php file being treated as if it were in the /apps/ directory? 所以我的问题是-如何正确编写.htaccess文件,以便home.phpapps.php页面可以是相同的文件并产生相同的结果,而不是将apps.php文件视为/apps/目录?

First, I should apologize as I don't have a solution which involves making changes in the htaccess. 首先,我应该道歉,因为我没有解决方案,其中涉及对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. 我认为,如果您有一个config变量(最好是在一个配置文件中)可以解决问题,该文件将保存图像,js等的根文件夹。大多数情况下,它的public_html,文档根目录,网站的URL指向至。 so your config variable could look like: 因此您的配置变量可能如下所示:

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

The config file should be included in index.php unconditionally. 配置文件应无条件地包含在index.php中。

So, when you include any js or images, you do it like this: 因此,当您包含任何js或图像时,您将像这样进行操作:

<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. 如果将配置文件包含在index.php中,则根据切换情况,所包含的所有文件都将能够使用$ base_url变量。

Another possible solution is to use the base tag. 另一种可能的解决方案是使用基本标签。 Look it up here: 在这里查找:

http://www.w3schools.com/TAGS/tag_base.asp 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,css和图片使用绝对网址(以斜杠开头)。

/js/main.js instead of js/main.js /js/main.js而不是js / main.js

You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). 除非进行外部重定向(通过将[R]标志添加到RewriteRule中),否则无法使用.htaccess进行操作。 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. 无法完成的原因:不是apps.php认为它在/ apps /目录中,而是浏览器认为。 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 . 在apps.php生成的页面源中,您将相对URL发送回浏览器,现在浏览器将请求相对于其所请求页面位置的这些资源。 For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side. 对于浏览器,无论您在服务器端内部进行了什么重写,它所获得的页面都位于/ apps /中。

So the options you have are: 因此,您有以下选择:

  • Do an external redirect with your .htaccess (and defeat your original purpose ;-) 使用.htaccess进行外部重定向(并破坏您的初衷;-)
  • Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs) 如您所说,在处理apps.php等的同时,用PHP动态更改URL(在URL前面加../)
  • Use absolute URLs, just as @nobody has suggested in his answer. 使用绝对URL,就像@nobody在他的答案中建议的那样。

The last one is the only real option IMHO. 最后一个是唯一的实物选项恕我直言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM