The page isn't redirecting properly when

Hello all,

Just wondering if I could get some help here. I’m almost finished with building my CMS system, and something that I been trying to get working is that if someone use the wrong url format, it would do a 301 redirect and carry on.

I am struggling to work out what I’m doing wrong.

When $config[‘blog_seo_enabled’] is set to on and I go to http://localhost/?p=test the url would change to http://localhost/test that is expected.

When I turn $config[‘blog_seo_enabled’] off, and I head to http://localhost/test I am getting redirected to http://localhost/?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=?p=test with firefox throwing up “The page isn’t redirecting properly” when I should be getting http://localhost/?p=test.

$page_url = request_var('p', '');

$page_url_rewrite = substr(rawurldecode($_SERVER['REQUEST_URI']),1);

if (preg_match('/^index\./', $page_url_rewrite))
{
				// The user probably wanted the home page
	header('HTTP/1.1 301 Moved Permanently');
	redirect($config['server_protocol'].$config['server_name'].'/');
}
if($page_url && $config['blog_seo_enabled'])
{
	//echo($config['server_protocol'].$config['server_name'].'/'.$page_url);
	//exit();
	// If seo links are enabled and someone trying to access via /?p=* we redirect then to the correct page (/*)
	header('HTTP/1.1 301 Moved Permanently');
	redirect($config['server_protocol'].$config['server_name'].'/'.$page_url);
}
elseif($page_url_rewrite && !$config['blog_seo_enabled'])
{

	//  If seo links are not enabled and someone trying to access via /* we redirect then to the correct page (/?p=*)
	header('HTTP/1.1 301 Moved Permanently');
	redirect($config['server_protocol'].$config['server_name'].'/?p='.$page_url_rewrite);
}

Could someone please let me know how I should be doing it?

My .htaccess looks like this

RewriteEngine On

RewriteRule ^a/([0-9\-]+)/?$ index.php?b&a=$1&seo=1 [QSA,NC]
RewriteRule ^s/\??author_id=([0-9]+)$ index.php?b&author_id=$1&seo=1&search [QSA,NC]
RewriteRule ^s/(.*)$ index.php?b&keywords=$1&seo=1&search [QSA,NC]

RewriteRule ^resources/([0-9]+)/(.*)$ index.php?downloadfile&i=$1&checkseo=$2 [QSA,NC]

RewriteRule ^rss/([a-z]+)\.html$ rss.php?mode=$1&seo=1 [QSA,NC]
RewriteRule ^rss/([a-z0-9-]+)/([a-z]+)\.html$ rss.php?b&ci=$1&mode=$2&seo=1 [QSA,NC]

RewriteRule ^c/([a-z0-9-]+)/$ index.php?b&ci=$1&seo=1 [QSA,NC]
RewriteRule ^c/([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?b&cp=$1&ci=$2&seo=1 [QSA,NC]
RewriteRule ^([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&p=$2&checkseo=$1 [QSA,NC]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&ci=$1&p=$3&checkseo=$2 [QSA,NC]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&cp=$1&ci=$2&p=$4&checkseo=$3 [QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  . index.php [L]

Thank-you :smile:

1 Like

Why in the name of all things holy are you not handling the all the routing logic at the application level like every other modern cms and framework does. That is just a gigantic mess when the only thing you need to do is use a rewrite rule for the front end controller allowing the application layer to handle the routing logic. In fact php league has an excellent package for doing just that. This will make the routing increasingly easier to debug. Not to mention that convoluted mess in my the htaccess file will be eliminated.

http://route.thephpleague.com/

The reason why that I’m doing things this way is because I’m merging 2 different systems (the other is a blog component) and the other system also doesn’t have any routing system in place. I’m building off phpBB 3.0 so there was never any routing system in place.

That makes sense.

It may help if you have a “temporary.log” just for this particular section and pass selected variables to the log:

// declare(strict_types=1); // IF AND ONLY IF PHP 7
error_reporting(-1);
ini_set('display_errors','1');

# REPLACEMENT
  $page_url   = request_var('p', '');
  $page_url_rewrite = substr(rawurldecode($_SERVER['REQUEST_URI']),1);
  if (preg_match('/^index\./', $page_url_rewrite))
  {
    $dst = "\n" . $config['server_protocol'].$config['server_name'].'/';
  }

  if($page_url && $config['blog_seo_enabled'])
  {
    $dst = "\n" . config['server_protocol'].$config['server_name'].'/'.$page_url;
  }
  elseif($page_url_rewrite && !$config['blog_seo_enabled'])
  {
    $dst = "\n" . $config['server_protocol'].$config['server_name'].'/?p='.$page_url_rewrite;
  }

# LOG ERRORS
  if(true):
    $itemsToLog = "\n" .'$PostItems: ' .implode(',', $_POST)
                . "\n" .'request_uri: ' .$_SERVER['REQUEST_URI']
                . "\n" .'destination: ' . $dst;   
    ini_set('error_log', __DIR__ .'/___TEMPORARY.log');
    error_log($itemsToLog ."\n");
    
    // DEBUG: REMOVE NEXT LINE OTHERWISE header(.  echo '<h3>ErrorLog:</h3>';
  highlight_file(__DIR__ .'/___TEMPORARY.log');
..); WILL NOT WORK
    echo '<h3>ErrorLog:</h3>';
    highlight_file(__DIR__ .'/___TEMPORARY.log');
  endif;

// REMOVE REMARK TO ENABLE header(...)
//  header('Location: ' . $dst, true, 301);
die;

Edit - added:
highlight_file(DIR .‘/___TEMPORARY.log’);

Thank-you for the help John_Betong,

This is what coming out of the log file. I added in comments showing when seo is on and off.

//when hitting localhost directly
[13-Mar-2016 17:12:07 Australia/Sydney]
$PostItems: 
request_uri: /
destination: 
//when hitting a webpage when seo is on
[13-Mar-2016 17:13:44 Australia/Sydney] 
$PostItems: 
request_uri: /test
destination: http://localhost/?p=test
[13-Mar-2016 17:14:57 Australia/Sydney] 
$PostItems: 
request_uri: /test
destination: http://localhost/?p=test

//when hitting a webpage when seo is off
[13-Mar-2016 17:15:50 Australia/Sydney] 
$PostItems: 
request_uri: /test
destination: http://localhost/?p=test


[13-Mar-2016 17:16:16 Australia/Sydney] 
$PostItems: 
request_uri: /test
destination: http://localhost/?p=test


[13-Mar-2016 17:20:06 Australia/Sydney] 
$PostItems: 
request_uri: /?p=test
destination: http://localhost/?p=?p=test

I’m starting to wonder if my original logic is flowed, also to make things harder, the computer that I’m writing this is on is a Windows 10 box with PHP 7, and I do intend to have this running on all sort of other servers with different PHP versions and settings.

Glad I was able to help.

Can you give a few examples of input Urls you expect and more importantly where you would like them to be routed.

Thank-you @John_Betong for getting back to me and sorry for the late reply. After taking time from this (so that I would be working with a fresh mind) @oddz is correct that I really should be using a routing system of some sort even though I’m merging 2 different systems together.

On the CMS side, the only input that there is, is pretty much a ?p=page url (like ?p=level1/page1, ?p=level1/page2, ?p=youcanuse/whateverpagenameyouwant. What would happen is that $page_url will get the value of “p=” parameter and then it will do a look up in the database, and if a match is found then it will output the page content from the database. If the page is not found then the system will return a 404 error. when wrouging I would like someone to just be able to type in domaine.test/youcanuse/whateverpagenameyouwant and it would be the same page as someone went to domaine.test/?p=youcanuse/whateverpagenameyouwant.

The blog side of things is more complex. Because ?p= is already used by the CMS, I wacked in a ‘b’ in the URL in order to make the main index script go to a different system altogether. So the url for a blog post looks like ?b&p=1,?b&p=2 and so on. What happens with p= is the same as what happens in the CMS, but we are checking on the post_id in the database.

The rest of the input are in the .htaccess that I’ve provided above, but I’ve included then below for your reference as well:

The below code is for the blog system:

RewriteRule ^a/([0-9\-]+)/?$ index.php?b&a=$1&seo=1 [QSA,NC] RewriteRule ^s/\??author_id=([0-9]+)$ index.php?b&author_id=$1&seo=1&search [QSA,NC] RewriteRule ^s/(.*)$ index.php?b&keywords=$1&seo=1&search [QSA,NC] RewriteRule ^resources/([0-9]+)/(.*)$ index.php?downloadfile&i=$1&checkseo=1 [QSA,NC] RewriteRule ^rss/([a-z]+)\.html$ rss.php?mode=$1&seo=1 [QSA,NC] RewriteRule ^rss/([a-z0-9-]+)/([a-z]+)\.html$ rss.php?b&ci=$1&mode=$2&seo=1 [QSA,NC] RewriteRule ^c/([a-z0-9-]+)/$ index.php?b&ci=$1&seo=1 [QSA,NC] RewriteRule ^c/([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?b&cp=$1&ci=$2&seo=1 [QSA,NC] RewriteRule ^([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&p=$2&checkseo=$1 [QSA,NC] RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&ci=$1&p=$3&checkseo=$2 [QSA,NC] RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)\-p([0-9]+)\.html$ index.php?view&cp=$1&ci=$2&p=$4&checkseo=$3 [QSA,NC]

The below code is for the CMS system:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  . index.php [L]

If I was to use a routing system, would I be able to stay with the template system that I’m using now and use the same DBAL that I’m using?

Hi Daniel,

I use a PHP Framework which is similar to a CMS because everything is routed to index.php as you well know.

I thought it was useful to pursue your problem because it could come in useful so…

Try this:

Online Redirection Demo

Source is at the bottom of the page.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.