14

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
Nijn
  • 388
  • 6
  • 22
  • do you have an htaccess file in parent folder that contains /subfolder/ ? – olegsv Aug 15 '16 at 20:54
  • I don't have an htaccess file in the parent folder. – Nijn Aug 16 '16 at 09:37
  • Could you post the full Notice error? It should have a file and line number. If so, post that file please :) – JayIsTooCommon Aug 17 '16 at 13:29
  • The full error is Notice: Undefined index: urlkey in /sites/domain.com/www/subfolder/chapter.php on line 4, this is where php tries to receive the chapter object using the urlkey. – Nijn Aug 18 '16 at 09:38
  • Can you put `LogLevel debug rewrite:trace3` to your apache configuration file ( not to .htaccess ), restart Apache and then post relevant log entries? – olegsv Aug 21 '16 at 15:03

3 Answers3

10

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?...

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

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.

Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
  • The name of the chapter is different for all chapters. Shouldn't this be different? Using you're code I receive the same error: Notice: Undefined index: urlkey in /sites/domain.com/www/subfolder/chapter.php on line 4. – Nijn Aug 18 '16 at 09:41
  • 1
    Hmm, I understand what you're saying and that the urlkey is not found is definitely the error. However, it works when the url is written fully, but not when it's rewritten using htaccess. Thus there must be an error in the htaccess file. – Nijn Aug 20 '16 at 07:07
1

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?.

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
  • Removing the RewriteBase doesn't change anything for me. It gives me the `Undefined index: urlkey` error – Nijn Aug 23 '16 at 16:04
  • 1
    The presence of the `RewriteBase` directive in this instance is entirely optional. – MrWhite Aug 23 '16 at 20:51