简体   繁体   English

在子文件夹中重写 .htaccess

[英]Rewriterule .htaccess in subfolder

I need to perform a rewrite in a subfolder using the .htaccess file.我需要使用 .htaccess 文件在子文件夹中执行重写。 The current url has a form like:当前 url 的形式如下:

domain.com/subfolder/chapter.php?urlkey=name

It needs to be rewritten to:需要改写为:

domain.com/subfolder/chapter/name

In the subfolder I have a .htaccess file with the following code:在子文件夹中,我有一个带有以下代码的 .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 is enabled, but for some reason when I go to the url Modrewrite 已启用,但出于某种原因,当我转到 url 时

domain.com/subfolder/chapter/name

It returns the following error:它返回以下错误:

Notice: Undefined index: urlkey

Have this code in subfolder/.htaccess :subfolder/.htaccess有此代码:

Options -MultiViews
RewriteEngine On
RewriteBase /subfolder/

RewriteRule ^chapter/([\w-]+)/?$ chapter.php?urlkey=$1 [L,NC,QSA]

Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html ) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files.选项MultiViews (参见http://httpd.apache.org/docs/2.4/content-negotiation.html )由Apache's content negotiation module ,该Apache's content negotiation modulemod_rewrite之前运行并使 Apache 服务器匹配文件的扩展名。 So if /file is the URL then Apache will serve /file.html .因此,如果/file是 URL,则 Apache 将提供/file.html

Also there is no need to rewrite to chapter?... you can rewrite to chapter.php?...也不需要改写成chapter?...可以改写成chapter.php?...

Your error is not related to htaccess rules.您的错误与 htaccess 规则无关。

Notice: Undefined Index注意:未定义索引

Happens when you try to access an array by a key that does not exist in the array.当您尝试通过数组中不存在的键访问数组时发生。

A typical example for an Undefined Index notice would be check sample code below. Undefined Index通知的典型示例是检查下面的示例代码。

$data = array('foo' => '42', 'bar');
echo $data['spinach'];
echo $data[1];

Both spinach and 1 do not exist in the array, causing an to be triggered. spinach1都不存在于数组中,导致触发 an。

The solution is to make sure the index or offset exists prior to accessing that index.解决方案是在访问该索引之前确保该索引或偏移量存在。 This may mean that you need to fix a bug in your program to ensure that those indexes do exist when you expect them to.这可能意味着您需要修复程序中的错误,以确保这些索引确实在您期望的时候存在。 Or it may mean that you need to test whether the indexes exist using array_key_exists or isset :或者这可能意味着您需要使用array_key_existsisset测试索引是否存在:

$data = array('foo' => '42', 'bar');
if (array_key_exists('spinach', $data)) {
    echo $data['spinach'];
}
else {
    echo 'No key spinach in array';
}

If you have code like:如果你有这样的代码:

<?php echo $_POST['message']; ?>
<form method="post" action="">
    <input type="text" name="message">
    ...

then $_POST['message'] will not be set when this page is first loaded and you will get the above error.那么$_POST['message']在这个页面第一次加载时不会被设置,你会得到上面的错误。 Only when the form is submitted and this code is run a second time will the array index exist.只有在提交表单并且第二次运行此代码时,数组索引才会存在。 You typically check for this with:您通常通过以下方式检查:

if ($_POST)  ..  // if the $_POST array is not empty
// or
if ($_SERVER['REQUEST_METHOD'] == 'POST') ..  // page was requested with POST

The notices above appear often when working with $_POST, $_GET or $_SESSION.当使用 $_POST、$_GET 或 $_SESSION 时,上面的通知经常出现。 For $_POST and $_GET you just have to check if the index exists or not before you use them.对于 $_POST 和 $_GET 你只需要在使用它们之前检查索引是否存在。 For $_SESSION you have to make sure you have the session started with session_start() and that the index also exists.对于 $_SESSION,您必须确保会话以 session_start() 开始并且索引也存在。

When removing RewriteBase it does working for me删除RewriteBase它对我RewriteBase

put this in subfolder please check把它放在子文件夹中,请检查

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]

and you have to add chapter.php?你必须添加chapter.php? instead of chapter?而不是chapter? . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM