doumengyin0491 2014-12-05 15:23
浏览 55

htaccess url重写规则无法按预期工作

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.

I have two files placed in the root directory - test.php and files_include.php.

The URL that a user would normally see is:

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:

www.example.com/search/A-B-C-D.html

Using a rule in .htaccess like this I can easily rewrite the 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:

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. 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. 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? The answer to this question will help me to identify the main issue, which is Problem2.

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.

Therefore I am guessing that by rewriting the URL to search/A-B-C-D.html I am breaking something with the inclusion of 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/A-B-C-D.html

Any suggestions?

  • 写回答

2条回答 默认 最新

  • drfu80954 2014-12-05 16:48
    关注

    Your browser is clueless about 'pretty' and 'ugly' urls. It just requests a folder or a file. If you request http://example.com/search/A-B-C-D.html, to the browser you are requesting a page A-B-C-D.html in the /search/ folder. If you have any relative urls on that page, it will request them relative to that /search/ folder. 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. An alternate solutions would be to add the <base> tag to the <head> tag of your page. The href property of this tag will be used as a base for any relative links on your page. See mdn for more information. You would then do:

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

    As for your second problem, the include itself is not the problem. include(..) will first try to find the file in the include_path, and otherwise in the script's directory and the working directory. This doesn't change if you create pretty urls. Apache, and php, still know where the actual file is located you are executing. 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. See the documentation.

    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败