Search code examples
apache.htaccessmod-rewrite

.htaccess rewrite rules behaving in a wrong way


I'm building an events website, in all my websites i do the following logic:

RewriteEngine On
Options +FollowSymLinks

RewriteBase /
RewriteRule ^new/event?(/)?$ controller.php?page_name=new-event [QSA,L]
RewriteRule ^get-event/([^/]+)?(/)?$ controller.php?page_name=get-event&event_id=$1 [QSA,L]
RewriteRule ^event/([^/]+)/([^/]+)?(/)?$ controller.php?page_name=event&event_id=$1 [QSA,L]
RewriteRule ^event-edit/([^/]+)?(/)?$ controller.php?page_name=event-edit&event_id=$1 [QSA,L]

// this below line to handle all links that do not apply to the above rules, is this wrong?
RewriteRule ^([^/]+)?(/)?$ controller.php?page_name=$1 [QSA,L]

The last line is to handle all URLs that don't have anything other than the page name, like those:

  • www.example.com/event-submit/

  • www.example.com/about/

  • www.example.com/contact/

Because I made a controller.php file that handles all the requests depending on the page_name value.

But this is not working with this new site, here's what i get:

when I request any URL belonging to this site, the browser just hangs and takes forever, sometimes the page loads but after like 5 minutes.

I have no idea why! (It works on all my other websites!)

Am I doing anything wrong? I have searched a lot and nothing is working.

EDIT

This is controller.php: (it just checks for page_name if it's allowed and then includes it)

<?php
session_start();
define('__SITE_ROOT__', dirname(__FILE__));
define('__HELPERS__', __SITE_ROOT__ . '/' . 'helpers');
define('__APP__', __SITE_ROOT__ . '/' . 'app');

$page_name = strip_tags($_GET['page_name']);

$allowed_pages = array(
    'index',
    'about',
    'contact',
    'new-event',
    'event-edit',
    'get-event',,
    '404'
);
class Controller{
    public $page_name = 'index';
    public $allowed_names = array();
}
$controller = new Controller();
$controller->allowed_names = $allowed_pages;
$controller->page_name = $page_name;
if(in_array($controller->page_name, $controller->allowed_names))
{
    $page_real_name = $controller->page_name . ".php";

}else{
    $page_real_name = "404.php";
}
include(__APP__  . '/' . $page_real_name);

Solution

  • // this below line to handle all links that do not apply to the above rules, is this wrong?
    RewriteRule ^([^/]+)?(/)?$ controller.php?page_name=$1 [QSA,L]
    

    This rule would create a rewrite-loop (500 error), since the regex ^([^/]+)?(/)?$ would also match controller.php (the URL being written to) and you don't appear to have anything that would otherwise prevent this?

    You could resolve this my adding a dot to the character class, so it won't match the dot in .php. For example:

    RewriteRule ^([^/.]+)?/?$ controller.php?page_name=$1 [QSA,L]
    

    You don't need to surround the last / in parentheses.

    (I'm assuming the // comment is just in your question, since that is syntactically invalid. Comments are delimited with #.)

    the browser just hangs and takes forever, sometimes the page loads but after like 5 minutes.

    However, the above doesn't quite explain this behaviour, unless maybe LimitInternalRecursion is set very high in your server config?! (Default is 10)