I cannot capture ROUTE without using index in URL

Asked

Viewed 218 times

1

In short, it only works if I put index.php in the URL. I think it’s in . htaccess, but I don’t know where. I wonder which configuration is incorrect or what could I do to find my error.

Data: Debian(9) Apache/2.4.25 (Debian) PHP 7.0.19-1

My . htaccess:

<IfModule mod.rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [NC, L]
</IfModule>

My index.php:

<?php
 $url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
 echo $url;
 //tentativa diferente
 $R = filter_input(INPUT_SERVER, 'REQUEST_URI');
 echo $R

Terminal:

$ systemctl status apache2.service
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2017-10-28 12:30:05 -02; 1h 8min ago
Process: 768 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)

$ a2enmod rewrite
Module rewrite already enabled

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

2 answers

0

The problem is in the flag delimiter:

 RewriteRule ^(.*)$ index.php/$1 [NC, L]
 #                                  ^^
 #                     não permite espaços aqui

The space is wrong. Switch to:

 RewriteRule (.*) index.php/$1 [NC,L]
  • Although you don’t really need to ignore upper and lower case with .*.


You can test your htaccess on .htcheck access.

  • With your case, it says "Fatal: Rewriterule: bad flag delimiters".

0

Apparently it’s OK your .htaccess. Maybe the directives Errordocument and Fallbackresource help complete what you intend to do (direct all routes, even non-existent resources, to index.php).

.htaccess

<IfModule mod_rewrite.c>

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

  ErrorDocument 404 /index.php
  FallbackResource /index.php

</IfModule>

index php.

<?php

   $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
   $query_string = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);

   echo "<strong>PATH:</strong>" . $path . "<br>";
   echo "<strong>QUERY STRING:</strong>" . $query_string . "<br>";

?>

Upshot:

path

  • Thanks @tom-Melo, your . htaccess worked. By comparison and testing I might find the error, my apache is not accepting two tags in the command within . htaccess, such as [NC L] or [QSA L]. I don’t know what there is but for now I just need the [L] tag. If someone knows how to set this up, please teach us, it might be useful in the future.

Browser other questions tagged

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