1

最近我开始在 php 中为 url 编写一个 url 路由脚本,如下所示:

http://www.example.com/messages/drafts

为了达到这个效果,我使用了一个包含以下内容的 .htaccess 文件:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule (.*) http://www.example.co/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

现在请求页面的模板是通过页面类加载的,然后在这些页面上有 switch 语句来确定要执行的命令......所以:

http://www.example.com/messages/drafts

将会; 加载messages.php 并执行drafts 命令,但您必须先登录才能执行此操作。因此,如果您未登录,它会将您重定向到登录页面并设置一个会话变量,其中包含错误消息和受限制的上一个页面。

由于某种原因,当 url 不仅仅包含 /messages 或 /user_home 等时,它会将会话变量设置为 /messages/img/bg.jpg...

结尾是登录页面上图像的路径。

我像这样设置会话$_SESSION['last_page'] = $_SERVER['REQUEST_URI'];,然后在其他地方验证它。

有谁知道为什么会发生这种情况?我认为问题出在 .htaccess 中。

页面加载完成后,我会回显请求 uri,它应该是这样的。

4

1 回答 1

1

I've had the same problem with a similar setup when using relative links. The problem is that Apache tries to process your background image the same way it does any other request. As a workaround I'm using absolute paths:

background-image: url:("http://yoursite.com/images/background.png");

instead of:

background-image: url:("/images/background.png");

I don't know if there's a more elegant solution, but for me this works just fine.

Clarification:

I mean to say your redirect is working, but then when the web server is parsing your login page, it treats the reference to your background image the same way it does any url written in the address bar.

Also there's a problem with relatives paths and url rewriting, since when you open www.yoursite.com/something and then have a link with the relative path /image.png, this is interpreted to mean www.yoursite.com/something/image.png

于 2013-07-31T10:35:07.637 回答