简体   繁体   English

htaccess网址重写规则未按预期工作

[英]htaccess url rewrite rule not working as expected

I have a complex problem that I an unable to solve for days now. 我有一个复杂的问题,现在几天都无法解决。 Maybe some expert with more knowledge of htaccess functionality will be able to help out. 也许一些对htaccess功能有更多了解的专家可以提供帮助。

I have two files placed in the root directory - test.php and files_include.php . 我有两个文件放在根目录- test.phpfiles_include.php

The URL that a user would normally see is: 用户通常会看到的URL是:

www.example.com/test.php?cs1=A&cs2=B&cs3=C&cs4=D

Since this is a ugly URL I would like to rewrite it to something better like: 由于这是一个丑陋的URL,因此我想将其重写为更好的形式:

www.example.com/search/ABCD.html

Using a rule in .htaccess like this I can easily rewrite the URL: 使用.htaccess这样的规则,我可以轻松地重写URL:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /test.php?cs1=$1&cs2=$2&cs3=$3&cs4=$4 [L]

In the file test.php I call for the website config files like this: 在文件test.php中,我这样调用网站配置文件:

include('files_include.php');

Now the problem . 现在的问题 As soon as I rewrite the URL to a location different from the root one, I get a really strange issue. 一旦将URL重写到不同于根目录的位置,就会遇到一个非常奇怪的问题。 The page still renders correct in browser but: 该页面在浏览器中仍然可以正确显示,但是:

Problem 1. I have to replace src="images with src="../images if I want to see the image correct. 问题1.如果我想查看正确的src="../images则必须用src="../images替换src="images This can be easily corrected by giving an absolute link, it is the easier part to do. 通过提供绝对链接可以很容易地纠正此问题,这是更容易完成的部分。

But the question is why is the relative path changing? 但是问题是为什么相对路径会改变? Is .htaccess making the browser think we are in search'/ folder? .htaccess是否使浏览器认为我们在search'/目录中? The answer to this question will help me to identify the main issue, which is Problem2. 该问题的答案将帮助我确定主要问题,即问题2。

Problem 2. Sitemaps generators cannot follow the links on the page once the URL is rewritten, as if it appears blank to them, no matter that in browser all looks fine. 问题2。一旦重写了URL,站点地图生成器就无法跟随页面上的链接,无论它们在浏览器中看起来是否正常,仿佛它们对它们而言都是空白。

Therefore I am guessing that by rewriting the URL to search/ABCD.html I am breaking something with the inclusion of files_include.php. 因此,我猜想是通过将URL重写为search/ABCD.html来实现的,其中包含files_include.php会破坏某些内容。

Basically, I need a general idea of were to look at and the things I should have in mind when rewriting root/test.php to root/search/ABCD.html 基本上,我需要一个大致的概念,即将root/test.php重写为root/search/ABCD.html时应注意的事项和注意root/search/ABCD.html

Any suggestions? 有什么建议么?

Your browser is clueless about 'pretty' and 'ugly' urls. 您的浏览器对“漂亮”和“丑陋”的网址一无所知。 It just requests a folder or a file. 它只是请求一个文件夹或文件。 If you request http://example.com/search/ABCD.html , to the browser you are requesting a page ABCD.html in the /search/ folder. 如果您向浏览器请求http://example.com/search/ABCD.html ,则您正在/search/文件夹中请求页面ABCD.html If you have any relative urls on that page, it will request them relative to that /search/ folder. 如果该页面上有任何相对URL,它将请求相对于该/search/文件夹的相对URL。 The browser has no clue, and should have no clue, what the internal representation of a request looks like. 浏览器没有任何线索,应该也没有任何线索,即请求的内部表示是什么样的。 Heck, at your end of the line it might even be translated to instructions for a colony of hamsters, which will then send correct data through. 哎呀,在这行的最后,它甚至可能被翻译成关于仓鼠的说明,然后仓鼠将通过它发送正确的数据。 The browser doesn't need to know how hamsters behave ;-) 浏览器不需要知道仓鼠的行为;-)

The first problem is easily resolved by making your urls absolute. 通过将您的网址设置为绝对网址,可以轻松解决第一个问题。 I wouldn't recommend making them relative to the pretty url. 我不建议使它们相对于漂亮的URL。 An alternate solutions would be to add the <base> tag to the <head> tag of your page. 另一种解决方法是将<base>标记添加到页面的<head>标记中。 The href property of this tag will be used as a base for any relative links on your page. 此标记的href属性将用作页面上任何相对链接的基础。 See mdn for more information. 有关更多信息,请参见mdn You would then do: 然后,您将执行以下操作:

<head>
  <base href="/">
</head>

As for your second problem, the include itself is not the problem. 至于第二个问题,include本身不是问题。 include(..) will first try to find the file in the include_path, and otherwise in the script's directory and the working directory. include(..)将首先尝试在include_path中查找文件,否则将在脚本目录和工作目录中查找文件。 This doesn't change if you create pretty urls. 如果您创建漂亮的网址,这不会改变。 Apache, and php, still know where the actual file is located you are executing. Apache和php仍然知道您正在执行的实际文件位于何处。 If an include statement fails to load a file it will generate an error too, which is another way you can tell if the include itself is the problem. 如果include语句无法加载文件,它也会产生错误,这是您可以判断include本身是否是问题的另一种方式。 See the documentation . 请参阅文档

But the question is why is the relative path changing? 但是问题是为什么相对路径会改变? Is .htaccess making the browser think we are in search'/ folder? .htaccess是否使浏览器认为我们在搜索目录中? The answer to this question will help me to identify the main issue, which is Problem2. 该问题的答案将帮助我确定主要问题,即问题2。

It's changing because the browser is loading /search/something-something-sometrhing-something.html instead of /test.php . 它正在发生变化,因为浏览器正在加载/search/something-something-sometrhing-something.html而不是/test.php The first URL has a relative URI base as: /search/ and the second URL has a base of / . 第一个URL的相对URI基为: /search/ ,第二个URL的基为/

For the second problem, you could try externally redirecting, but not sure if that'll help the sitemap itself, it depends on the generator. 对于第二个问题,您可以尝试进行外部重定向,但不确定是否会对站点地图本身有所帮助,这取决于生成器。 Try adding this rule: 尝试添加以下规则:

RewriteCond %{THE_REQUEST} \ /+test\.php\?cs1=([^&]*)&cs2=([^&]*)&cs3=([^&]*)&cs4=([^&\ ]*)
RewriteRule ^ /search/%1-%2-%3-%4.html [L,R]

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

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