1

当我使用此 htaccess 代码时,我从链接的 CSS 文件等中收到 500 内部服务器错误。有人知道可能是什么问题吗?我还不太熟悉htaccess。

这是代码:

RewriteEngine On
RewriteBase /

RewriteRule ^(system|img|res) - [L]

RewriteRule ^picture/([^/]*)/?$ picture.php?id=$1 [L,QSA]

## The below code is something I found on the internet to remove the .php tag
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

# remove index
RewriteRule (.*)index$ $1 [R=301]

# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

URL 应该是:www.mysite.com/pictures/1 (id)

id 始终是一个数字。

它确实向我显示了页面,并且我可以回显 ID,因此该部分正在工作,但是如上所述,它在链接文件上给了我 500 错误。

4

1 回答 1

1

不知道为什么会这样。CSS文件夹与实际的php文件位于同一文件夹中。

您已使用相对 URI 链接到它:

<link rel="stylesheet" type="text/css" media="all" href="./css/text.css" />

例如./css/text.css, 虽然 css 文件可能与文件位于同一目录中picture.php(我认为这是生成内容的内容),但浏览器实际上是对 CSS 的请求,而不是 picture.php 脚本。浏览器请求这个 URL http://www.mysite.com/picture/1,并且服务器内部将其重写/picture/1/picture.php?id=1,浏览器不知道发生了什么。所以它将基本 URI 视为/picture/. 如果浏览器直接转到 php file: http://www.mysite.com/picture.php?id=1,则基本 URI 将是/并且 css 将很好地解析为/./css/text.css. 但是/picture/1请求具有不同的基础 URI,因此浏览器(不知道基础不同)盲目地尝试将 css 检索为/picture/./css/text.css,失败是因为您有错误处理该 URI 的规则。通常你只会得到一个 404,但是你在图片重写之后的规则会错误地处理 URI 并返回一个 500 服务器错误。

您可以在标题中添加:

<base href="/">

在由 生成的内容中picture.php,或者使 URI 的绝对值:

<link rel="stylesheet" type="text/css" media="all" href="/css/text.css" />
于 2012-10-01T19:56:36.447 回答