I need to perform a rewrite in a subfolder using the .htaccess file. The current url has a form like:
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:
<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
domain.com/subfolder/chapter/name
It returns the following error:
Notice: Undefined index: urlkey
You can add a . htaccess file to any directory that requires lenient permissions settings (such as 760, 766, 775 or 777). You can prevent the execution of scripts inside the directory and all its sub-directories.
You can redirect all requests to a subdirectory by adding an . htaccess file to the root of your domain's directory: Visit the FTP page for instructions on how to upload. Once connected, upload (or create) a text file named .
htaccess rewrite rule includes setting a combination of rewrite condition ( RewriteCond ) tests along with a corresponding rule ( RewriteRule ) if the prior conditions pass. In most cases, these rules should be placed at any point after the RewriteEngine on line in the . htaccess file located in the website's docroot.
RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.
Have this code in 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. So if /file
is the URL then Apache will serve /file.html
.
Also there is no need to rewrite to chapter?...
you can rewrite to chapter.php?...
Your error is not related to htaccess rules.
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.
$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.
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
:
$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. 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. For $_POST and $_GET you just have to check if the index exists or not before you use them. For $_SESSION you have to make sure you have the session started with session_start() and that the index also exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With