[英]Htaccess url rewrite, new folder, but files load in parent folder
My .htaccess
file changes http://example.com/animal.php?id=number
to http://example.com/animal/number
. 我的
.htaccess
文件将http://example.com/animal.php?id=number
更改为http://example.com/animal/number
。
If I go to http://example.com/animal/number
, this code loads animal.php
's other files in /animal
folder, but this folder doesn't exist. 如果我访问
http://example.com/animal/number
,此代码会将animal.php
的其他文件加载到/animal
文件夹中,但此文件夹不存在。
I would like for it to work if I go to /animal/number
page, the code will load animal.php
's other files in parent folder ( http://example.com/
). 如果我去
/animal/number
页面,我希望它可以工作,代码将加载animal.php
在父文件夹( http://example.com/
)中的其他文件。
Example: I'm including header.php
in animal.php
, and this is working, but if I go to /animal/number
page, this is not working, because header.php
not in the /animal
folder. 示例:我在
animal.php
包含header.php
,这是有效的,但如果我转到/animal/number
页面,这不起作用,因为header.php
不在/animal
文件夹中。
My .htaccess
file: 我的
.htaccess
文件:
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteBase /htdocs/test
RewriteRule ^animal/([A-Za-z0-9-]+)/?$ animal.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA]
</IfModule>
What you appear to describe is a client-side relative URL issue. 您似乎描述的是客户端相对URL问题。 If you are using relative URLs in your client-side HTML then any resources (CSS, JS, images, etc) are naturally requested relative to the
/animal
"subdirectory" (as specified in the URL), not the document root (the location of the actual document). 如果您在客户端HTML中使用相对 URL,那么相对于
/animal
“子目录”(在URL中指定),而不是文档根(位置),自然会请求任何资源(CSS,JS,图像等)实际文件)。 The browser only sees the URL, not the underlying filesystem path. 浏览器只能看到URL,而不是底层文件系统路径。
You would need to resolve this by either using root-relative URLs (starting with a slash) or absolute URLs (scheme + hostname) in your client-side HTML. 您需要通过在客户端HTML中使用根相对URL(以斜杠开头)或绝对URL(方案+主机名)来解决此问题。 Alternatively you can set a
base
element in the head
section of your HTML that tells the browser what URL to use as the base URL when resolving relative links/URLs. 或者,您可以在HTML的
head
部分设置一个base
元素,告诉浏览器在解析相对链接/ URL时要使用哪个URL作为基本URL。 (However, note that there are caveats with using the base
element if you are using in-page links with just a fragment identifier, eg. of the form href="#internal-id"
- since this is considered a relative URL.) (但是,请注意,如果您使用仅具有片段标识符的页内链接(例如,形式为
href="#internal-id"
,则使用base
元素存在警告 - 因为这被视为相对 URL。)
I'm including
header.php in
animal.php`我
header.php in
animal.php`中包含了header.php in
HOWEVER... "including header.php
" implies a server-side include (eg. include('header.php')
), which has nothing to do with the URL structure, relative URLs (in the HTML) and hence nothing to do with URL-rewriting and your .htaccess
file. 但是......“包括
header.php
”意味着服务器端包含(例如include('header.php')
),它与URL结构,相对URL(在HTML中)无关,因此没有任何关系使用URL重写和.htaccess
文件。
Unless... you are including this as some kind of JavaScript AJAX request (or even a frame )? 除非......你把它包含在某种JavaScript AJAX请求中(甚至是一个框架 )? (Hence my query in the comments above.) In this case, the same relative URL problem would occur as mentioned above.
(因此我在上面的评论中查询。)在这种情况下,如上所述会出现相同的相对URL问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.