[英]Getting a 500 Internal Server Error when rewriting 2 specific URLs
在嘗試重寫這些特定的 URL/文件(item.php 和 purchase.php)時,我不斷收到 500 內部服務器錯誤,其余的都有效。
我嘗試了很多方法來解決這個問題,但似乎沒有任何效果,這很奇怪,因為所有其他 URL 都可以工作,但是這 2 個似乎出於某種原因不想工作。
.htaccess 文件
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ErrorDocument 500 "500"
ErrorDocument 404 "404"
ErrorDocument 403 "403"
RewriteRule ^users/([^/]*)$ /user/profile.php?username=$1
RewriteRule ^users/([^/]*)/inventory$ /user/GetUserInventory.php?username=$1
RewriteRule ^market/item/([^/]*)$ /market/item.php?id=$1
RewriteRule ^market/item/([^/]*)/purchase$ /market/purchase.php?id=$1
RewriteRule ^community/([^/]*)$ /communities/view.php?id=$1
RewriteRule ^community/([^/]*)/join$ /communities/join.php?id=$1
RewriteRule ^community/([^/]*)/manage$ /communities/manage.php?id=$1
RewriteRule ^game/([^/]*)$ /games/view.php?id=$1
項目.php
<?php
include_once('../private/header.php');
$item = $handler->query("SELECT * FROM items WHERE id=" . $_GET['id']);
$gB = $item->fetch(PDO::FETCH_OBJ);
echo '
<div class="col s12 m9 l8">
<div class="container" style="width:100%;">
<div class="content-box" style="border-radius:0;">
<div class="left-align">
</div>
<div class="row">
<div class="col s12 m6 l3 center-align">
<img src="'.$cdnServer.'/items/thumbnails/'.$gB->image.'.png" class="responsive-img">
</div>
<div class="col s12 m6 l6">
<div style="padding-left:25px;overflow:hidden;">
<div style="font-size:26px;font-weight:300;">'.$gB->name.'
<b style="text-transform:uppercase;font-size:12px;">'.$gB->type.'</b>
</div>
<div style="color:#777;font-size:14px;">'.$gB->description.'</div>
</div>
</div>
<div class="col s12 m3 l3 center-align" style="padding-top:15px;">
<center>
';
if ($gB->onsale == 1){
echo '<a href="'.$serverUrl.'/market/item/'.$gB->id.'/purchase" class="modal-trigger waves-effect waves-light btn green">Purchase</a>';
} else {
echo '<a class="modal-trigger waves-effect waves-light btn grey darken-2">Offsale</a>';
}
echo '
</center>
<div style="height:15px;"></div>
<center><b style="text-transform:uppercase">Creator</b></center>
<center><a href="'.$serverUrl.'/users/'.$gB->creator.'" style="padding-top:12px;font-size:16px;display:inline-block;">'.$gB->creator.'</a></center>
';
if($gB->collectable == 'true'){
if($gB->amount == 0)
echo '
<center><span style="color:red">Sold Out</span></center>
';
}else{
echo '
<center><span style="color:red">'.$gB->amount.' Remaining</span></center>
';
}
echo '
<div style="height:25px;"></div>
</div>
</div>
<div style="padding-top:25px;">
<div class="row" style="margin-bottom:0;">
<div class="col s12 m12 l3 center-align">
<div style="font-size:20px;">'.$gB->created.'</div>
<div style="color:#999;font-size:14px;">Time Created</div>
</div>
<div class="col s12 m12 l3 center-align">
<div style="font-size:20px;">'.$gB->created.'</div>
<div style="color:#999;font-size:14px;">Last Updated</div>
</div>
<div class="col s12 m12 l3 center-align">
<div style="font-size:20px;">???</div>
<div style="color:#999;font-size:14px;">Owners</div>
</div>
</div>
</div>
</div>
';
include_once('../private/footer.php');
購買.php
<?php
include_once('../private/config.php');
if ($user){
$money=$myu->CurrencyCoins;
$id=$_GET['id'];
$item = $handler->query("SELECT * FROM items WHERE id=" . $id);
$gB = $item->fetch(PDO::FETCH_OBJ);
$amount=$gB->amount;
if ($gB->onsale == 1){
if ($money >= $gB->price){
if ($gB->collectable != "true"){
if ($amount != 0){
$new = ($money - $gB->price);
$handler->query("UPDATE `users` SET `CurrencyCoins`='".$new."' WHERE `id`='".$myu->id."'");
$handler->query("INSERT INTO inventory (item,user) VALUES (".$id.",".$myu->id.")");
}
} else {
if ($amount >= 1){
$amount1=($amount - 1);
$new = ($money - $gB->price);
$handler->query("UPDATE `users` SET `CurrencyCoins`='".$new."' WHERE `id`='".$myu->id."'");
$handler->query("UPDATE `items` SET `amount`='".$amount1."' WHERE `id`='".$gB->id."'");
$handler->query("INSERT INTO inventory (item,user) VALUES (".$id.",".$myu->id.")");
} else {
echo '<center><h2>Item is sold out!</h2></center>';
}
}
}
} else {
echo '<center><h2>Item not on sale!</h2></center>';
}
}
echo '<head><meta http-equiv="refresh" content="1; url='.$serverUrl.'/account/character"></head>';
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\\.php -f RewriteRule ^(.*)$ $1.php
這些指令應該在你的.htaccess
文件的末尾,在其他重寫之后。 它們也是不正確的 - 盡管將它們放在文件末尾可以避免直接問題,但仍然可能導致其他 URL 出現問題。
在當前狀態下,當您請求example.com/market/item/1
(看起來, /market
是一個物理目錄,而/item.php
是該目錄中的一個文件)然后...
簡而言之......它會導致無限的重寫循環:
/market/item/1.php
/market/item/1.php.php
/market/item/1.php.php.php
/market/item/1.php.php.php.php
etc.
這會中斷(在 10 次迭代后),並向瀏覽器發送 500 響應。
請求/market/item/1/purchase
時會發生非常相似的過程:
/market/item/1/purchase.php
/market/item/1/purchase.php.php
/market/item/1/purchase.php.php.php
/market/item/1/purchase.php.php.php.php
etc.
詳細...
REQUEST_FILENAME
是/market/item
(忽略目錄前綴), PATH_INFO
是/1
(以后很重要)。 /market/item
不是目錄(第一個條件),但/market/item.php
是一個文件(第二個條件) - 所以兩個條件都成功滿足。
RewriteRule
指令然后將/market/item/1
重寫為/market/item/1.php
(顯然不正確,而不是意圖)。 由於此規則上沒有L
標志,處理繼續......為了其余規則, PATH_INFO
(來自初始請求, /1
)被附加到結果 URL 成為/market/item/1.php/1
( DPI
標志 - 丟棄路徑信息 - 是為了防止這種特定行為而創建的)。
/market/item/1.php/1
不匹配當前傳遞中的任何其他規則,因此重寫引擎從/market/item/1.php
的頂部重新開始。
REQUEST_FILENAME
是/market/item
而PATH_INFO
現在是/1.php
。 /market/item
不是目錄(第一個條件),但/market/item.php
是一個文件(第二個條件)-因此第二次成功滿足這兩個條件。
該RewriteRule
指令然后重寫/market/item/1.php
到/market/item/1.php.php
。 由於此規則上沒有L
標志,處理繼續......為了其余規則, PATH_INFO
(來自請求,這次是/1.php
)被附加到結果 URL 中成為/market/item/1.php.php/1.php
。
/market/item/1.php.php/1.php
不匹配當前傳遞中的任何其他規則,因此重寫引擎從頂部重新開始/market/item/1.php.php
。
GOTO #4(帶有更新的 URL 路徑)等等等等。重寫循環,500 錯誤。
請求/market/item/1/purchase
時會發生非常相似的過程。 REQUEST_FILENAME
與/market/item
相同(因此它再次檢查/market/item.php
存在,而不是purchase.php
),除了PATH_INFO
是/1/purchase
(不是/1
)。 .php
擴展名附加到的初始 URL 路徑自然是/market/item/1/purchase
(不是/market/item/1
)。
如果您遵循了“令人困惑的混亂”,您將看到檢查“path/to/file”+“.php”是否存在的條件不一定與實際重寫請求的規則相同到“網址路徑”+“.php”。 (“path/to/file”與“URL-path”不同)。 為了解決這個問題,它應該寫成:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
這里不需要進行目錄檢查,因為如果它是一個目錄,那么接下來的文件檢查一定會失敗(除非您的目錄名稱以.php
)。 %{DOCUMENT_ROOT}/$1.php
檢查現在實際上與$1.php
(被重寫的文件)“相同”。
文字點不需要在RewriteCond
TestString 中轉義反斜杠 - 這是一個“普通”字符串,而不是正則表達式。
不要忘記L
標志。 這個規則塊現在應該放在.htaccess
文件的末尾。
Options -Indexes
RewriteEngine on
ErrorDocument 500 "500"
ErrorDocument 404 "404"
ErrorDocument 403 "403"
RewriteRule ^users/([^/]*)$ /user/profile.php?username=$1 [L]
RewriteRule ^users/([^/]*)/inventory$ /user/GetUserInventory.php?username=$1 [L]
RewriteRule ^market/item/([^/]*)$ /market/item.php?id=$1 [L]
RewriteRule ^market/item/([^/]*)/purchase$ /market/purchase.php?id=$1 [L]
RewriteRule ^community/([^/]*)$ /communities/view.php?id=$1 [L]
RewriteRule ^community/([^/]*)/join$ /communities/join.php?id=$1 [L]
RewriteRule ^community/([^/]*)/manage$ /communities/manage.php?id=$1 [L]
RewriteRule ^game/([^/]*)$ /games/view.php?id=$1 [L]
# Append .php on remaining requests
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.