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
3 Answers
Answers 1
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?...
Answers 2
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.
Answers 3
When removing RewriteBase
it does working for me
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?
instead of chapter?
.
0 comments:
Post a Comment