Rewriterule problems in apache 2.22 (in Digitalocean.Comm)

Asked

Viewed 179 times

1

I use a cloud server called Digital Ocean. However I am facing problems while trying to create friendly Urls. I have activated the module rewrite through the command:

sudo a2enmod rewrite

and altered sudo nano /etc/apache2/sites-available/default leaving so:

<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
 </Directory>

Only if, by chance, there is a file with the same name it does not pass through my .htacess, irrespective of the extent

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ sessao.php [NC,L,QSA]

Here too is mine sessoes.php:

$gets = explode("/",str_replace(strrchr($_SERVER["REQUEST_URI"], "?"), "", $_SERVER["REQUEST_URI"]));

for($i=1; $i<=count($gets); $i++) {
    $var = "p$i";
    $$var = addslashes($gets[$i]);
}
if (file_exists($p1.".php")) {
    include("teste.$p1".".php");
} else {
    header("Location: /");
    exit();
}

Practical example of the situation: If I call http://107.170.237.146/base he carries the http://107.170.237.146/base.html if you find the same, if you do not find it search http://107.170.237.146/base.php, if you do not find it carries the sessoes.php.

But I can verify that it always go through first sessoes.php and then drop . html or in . php.

How can I make him first obey the .htaccess ?

1 answer

1

I’m not sure I understand very well the use of sessoes.php that you are presenting.

From what I saw she is just treating how the request was made to then make include of the respective file.

For this purpose I would directly use . htaccess making in it the rule of directing to the correct file.

Ex:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /sessao.php?file=$1 [NC,L,QSA]

So calling http://107.170.237.146/base.html who will actually open is sessao.php receiving via $_GET the "file" parameter with the "base" value"

And this rule will only work if there is no file called base.html at the root of your webserver, because of the condition:

RewriteCond %{REQUEST_FILENAME} !-f

What translating says that the request cannot be a file.

If it’s not, let me know, I’ve already implemented several situations with htaccess, and I might be able to help you with your problem.

Browser other questions tagged

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