![](/img/trans.png)
[英].htaccess RewriteRule for one IP only (with regex subfolder)
[英]Rewriterule .htaccess in subfolder
我需要使用 .htaccess 文件在子文件夾中執行重寫。 當前 url 的形式如下:
domain.com/subfolder/chapter.php?urlkey=name
需要改寫為:
domain.com/subfolder/chapter/name
在子文件夾中,我有一個帶有以下代碼的 .htaccess 文件:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /subfolder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^chapter/([a-z0-9]+)/?$ chapter.php?urlkey=$1 [L,NC,QSA]
</IfModule>
Modrewrite 已啟用,但出於某種原因,當我轉到 url 時
domain.com/subfolder/chapter/name
它返回以下錯誤:
Notice: Undefined index: urlkey
在subfolder/.htaccess
有此代碼:
Options -MultiViews
RewriteEngine On
RewriteBase /subfolder/
RewriteRule ^chapter/([\w-]+)/?$ chapter.php?urlkey=$1 [L,NC,QSA]
選項MultiViews
(參見http://httpd.apache.org/docs/2.4/content-negotiation.html )由Apache's content negotiation module
,該Apache's content negotiation module
在mod_rewrite
之前運行並使 Apache 服務器匹配文件的擴展名。 因此,如果/file
是 URL,則 Apache 將提供/file.html
。
也不需要改寫成chapter?...
可以改寫成chapter.php?...
您的錯誤與 htaccess 規則無關。
注意:未定義索引
當您嘗試通過數組中不存在的鍵訪問數組時發生。
Undefined Index
通知的典型示例是檢查下面的示例代碼。
$data = array('foo' => '42', 'bar');
echo $data['spinach'];
echo $data[1];
spinach
和1
都不存在於數組中,導致觸發 an。
解決方案是在訪問該索引之前確保該索引或偏移量存在。 這可能意味着您需要修復程序中的錯誤,以確保這些索引確實在您期望的時候存在。 或者這可能意味着您需要使用array_key_exists
或isset
測試索引是否存在:
$data = array('foo' => '42', 'bar');
if (array_key_exists('spinach', $data)) {
echo $data['spinach'];
}
else {
echo 'No key spinach in array';
}
如果你有這樣的代碼:
<?php echo $_POST['message']; ?>
<form method="post" action="">
<input type="text" name="message">
...
那么$_POST['message']
在這個頁面第一次加載時不會被設置,你會得到上面的錯誤。 只有在提交表單並且第二次運行此代碼時,數組索引才會存在。 您通常通過以下方式檢查:
if ($_POST) .. // if the $_POST array is not empty
// or
if ($_SERVER['REQUEST_METHOD'] == 'POST') .. // page was requested with POST
當使用 $_POST、$_GET 或 $_SESSION 時,上面的通知經常出現。 對於 $_POST 和 $_GET 你只需要在使用它們之前檢查索引是否存在。 對於 $_SESSION,您必須確保會話以 session_start() 開始並且索引也存在。
刪除RewriteBase
它對我RewriteBase
把它放在子文件夾中,請檢查
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^chapter/([a-z0-9]+)/?$ chapter.php?url=$1 [L,NC,QSA]
你必須添加chapter.php?
而不是chapter?
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.