error help . htaccess

Asked

Viewed 144 times

1

good people my question this time and the next I’m doing the system of url amigável in php I’ve already done the .htaccess that has remained so:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=$1

and then I enabled the rewrite in apache and saved all my navigation files in a folder called nav leaving only the index out of it on my index right at the beginning of the page I did the following code:

$pag = (isset($_GET['pag'])) ? $_GET['pag'] : 'default';
$pasta = 'nav';
    if(file_exists("$pasta/$pag.php")){
        include("$pasta/$pag.php");
    }else{
        header("location: error/404.html");
    }

but now when I try to get into my index.php or just typing the path of my folder where this saved the files it does not let me enter always falls on the error page someone could help me?

  • Hello Leonardo, have you checked the apache configuration file if it supports Override? If it does not support, these settings have to be in the site’s configuration file (i.e. htaccess will not work)

  • yes I checked!

  • Leonardo if the Nav folder is above Document root, don’t you have to touch that file_exists there? It would have to be '.. /'. $folder/pag.php

2 answers

0

You can do it this way too:

$pag = (isset($_GET['pag'])) ? $_GET['pag'] : 'default';
$pasta = "nav";
    if(file_exists($_SERVER['DOCUMENT_ROOT']."/{$pasta}/{$pag}.php")){
        include($_SERVER['DOCUMENT_ROOT']."/{$pasta}/{$pag}.php");
    } else{
        header("location: error/404.html");
    }



RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
  • it is always entering directly already in the friend error folder both my code and your Windows type the path of the site it already falls in the error page

  • edited my answer try to see

  • keeps falling on the error page

  • If I try to put in I’m the href of the index it appears to me that this giving loop redirecting

  • your logic is incorrect there in PHP

  • where this incorrect could point me?

Show 1 more comment

0


I do not know the logic will be the same, but because there is no example of the part that contains the html from your example, I created a very simple.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p><a href="home-10.html" target="_blank">Home</a> | <a href="contacto-12.html" target="_blank">Contacto</a> | <a href="ajuda-14.html" target="_blank">Ajuda</a></p>
<?php
 $p = isset($_GET['pag']) ? $_GET['pag'] : 'home';
 if(isset($p)){ 
    $pasta = 'temp'; 
    if(file_exists($pasta . '/' . $p .'.php')){
        include_once $pasta . '/' .$p . '.php';
        } else {
        header('location: error/404.html'); 
        exit();
        }
    }
 ?>
</body>
</html>

As you can see, the part that contains the PHP remained unchanged, except in some parts, yet most of it prevails. The file .htaccess is in the directory root of the example in use, together with the index.php, containing the above code. The links were written by me, and are not generated dynamically from a database, or any other source, and I put in them a target="_blank" for ease when testing. My file home php. is equivalent to your file default.

In the same directory, I created two more folders, respectively error and temp.

Root

  • .htaccess
  • index php.
  • error/
  • temp/

In the folder error saved a file with the name 404.html containing the error message displayed whenever the page is not found.

In the folder temp saved the files home php. and php contact. containing respectively the code <?php echo "Página principal"; ?> and <?php echo "Página de contacto"; ?>.

The file .htaccess I used, it’s just like yours, except I just changed the expression to something more specific.

RewriteEngine On
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z]+)$ index.php?pag=$1
RewriteRule ^([A-Za-z]+).html$ index.php?pag=$1
RewriteRule ^([A-Za-z]+)-([0-9]+).html$ index.php?pag=$1

This way I can write the links in 3 different ways.

  • home
  • home html.
  • home-10.html (the type of link that I used in this example).

#Edit

could make me doubt where this my code is located in index.php it also and the main part of my code and I noticed that you used home that your home equivalent to my index.php in case I would change my default to index or your home to index to adapt it to my site?

In this case, where do you have if(file_exists(...)){ #instrução# }, you’d have to put the code down instead of #instrução#. However, if you worked with includes, I could make it easier for you in a lot of ways, and you’d better organize the content.

#   
if($p === 'home'){
/*
 * Retorno ou mensagem a ser exibida na página pricipal, quando nenhuma outra está selecionada
*/
echo "Página principal"; 
} else {
include_once $pasta . '/' .$p . '.php'; 
}
#

#Edit 2

Replace all the php code in your index.php and put this in place of the old code:

$p = isset($_GET['url']) ? $_GET['url'] : 'default';
 if(isset($p)){ 
    $pasta = 'temp'; 
    if(file_exists($pasta . '/' . $p .'.php')){
        #   
        if($p === 'default'){
            /*
             * Retorno ou mensagem a ser exibida na página pricipal, quando nenhuma outra está selecionada
             */
            echo "Página principal"; 
        } else {
            include_once $pasta . '/' .$p . '.php'; 
        }
        #
        } else {
        if($p === 'default'){
            echo "Página principal";    
        } else {
            header('location: error/404.html'); 
            exit(); 
        }
        }
    }

Although this solves your current problem, I do not recommend this practice. There are many and many ways to do this with pleasant results. I recommend you find out more about Friendly Urls.

  • could make me doubt where this my code is located in the index.php he also is the main part of my code and I noticed that you used home that your home is equivalent to mine index.php in case I would change my default to index or your home to index to adapt it to my site?

  • Look I edited my answer, read the part #Edit, and see if this solves that problem.

  • guy I guess you didn’t mean what I asked Quiz I’ll ask in an easier way this home you’re calling on isset he and a file home.php or and a folder called home?

  • It’s a file home.php.

  • he then where Voce put home in isset I put index and that neh?

  • I changed mine to index but unfortunately not changed anything it keeps returning to the error page not opening the index.php

  • What value are you using here: $p = isset($_GET['pag']) ? $_GET['pag'] : 'home'; ? Home or Default ?

  • before it was default and did not catch now I switched to index and does not catch too

  • I edited the answer again, do the test and say what returned now.

  • now gave no error it returned home page

  • Everything is in order ?

  • looks but and these echo main page I can take them?

  • Where is echo "Pagina principal", is where the contents of the main page would be, you can remove them and place the content you want displayed on the main page. But that way you’ll have a code that’s too long and a little confusing.

  • intendi I’ll see what I can do to reduce that code

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.