B. Dionys
B. Dionys

Reputation: 916

htaccess if folder exists check folder

I am learning htaccess files at this moment and ive come across an issue I cannot seem to solve.

At this moment im using this htacces to make a clean url:

RewriteEngine on
RewriteRule ^([0-9a-z-A-Z]+)/([0-9a-z-A-Z]+) ?menu=$1&page=$2.php [NC,L]

I have 2 possible file locations:

  1. /menu/page
  2. /menu/Controller/Page

I want both of them to just show: /menu/page in the URL

So I did that. It shows what I want in the url. But becouse im referring in my menu to the second as /menu/Controller/Page I get this error:

Warning: include(Partials/Programmeren/Opdracht1Restwaarde.php): failed to open stream: No such file or directory on line 20.

This is my index page:

<?php
session_start();
if( isset($_GET['page']) ){
    //Heres a problem I know this. I only dont know how to fix it.
    $cPagina = $_GET['menu'] . '/' .$_GET['page'];
} elseif( isset($_SESSION['laatstGebruiktePagina']) ) {
    $cPagina = $_SESSION['laatstGebruiktePagina'];
} else {
    $cPagina = 'indexcontent.php';
}
$_SESSION['laatstGebruiktePagina'] = $cPagina;

include "Partials/head.php";
include "Partials/navbar.php";

//problem include:
include("Partials/$cPagina");
?>

So at the point in the above php code I have a place where I specify the location of the file. but sometime it should be: $cPagina = $_GET['menu'] . '/controller/' .$_GET['page'];. At first I thought i could simply do this with a if statement. but I cant becouse there is nothing to check it with.

ADDITION: Im not necessarily asking for some code that fixes this for me. Even a point in the right direction on where I can read this or something is appreciated. Im reading about htaccess at the apache site and some other tutorials at this moment

Upvotes: 1

Views: 46

Answers (1)

anubhava
anubhava

Reputation: 785128

You can change your rule to this:

RewriteEngine On

RewriteRule ^(.+)/([0-9a-z]+)/?$ ?menu=$1&page=$2.php [NC,L]

This will take care of both these cases:

/menu/page
/menu/Controller/Page

and forward those to:

?menu=menu&page=page.php
?menu=menu/Controller&page=page.php

respectively so that you can continue to use:

$cPagina = $_GET['menu'] . '/' .$_GET['page'];

in your code.

Upvotes: 1

Related Questions