Bonjour,

Tout d'abord, je ne sais pas si je suis dans la bonne section. Si ce n'est pas le cas, j'en suis désolé :)

J'ai un .htaccess avec le code suivant:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?router=$1 [L,QSA]

Mon QUERY_STRING contient router=index/13/sdfsdfsdf.txt pour l'url suivante : http://localhost/projet/public/index/13/sdfsdfsdf.txt

Le problème c'est que j'obtient le même résultat pour l'url http://localhost/projet/public/index/13////sdfsdfsdf.txt
hors cette url n'est pas valide.

Je pense que l'erreur vient de ma règle de réécriture mais je peux me tromper.
Vous avez une idée de comment résoudre le problème ?

Merci et bonne soirée

7 réponses


yanis-git
Réponse acceptée

Alors. La partie qui nous interesse est la suivante :


  'QUERY_STRING' => string 'router=index/13/sdfsdfsdf.txt' (length=29)
  'REQUEST_URI' => string '/projet/public/index////13/sdfsdfsdf.txt' (length=51)

C'est très surprenant comme comportement, mais il semblerait que Apache modifie ta query string avant de le faire le rewrite (peut être au niveau des flags de réécriture [L,QSA] ?).

Si tu ne trouves pas l'origine de ce comportement, tu peux donc te baser sur le REQUEST_URI pour opérer au routing coté PHP.

Bonjour, tu es dans la bonne section rassure toi.
Ton Rewrite Url est bon. Le reste se passe coté PHP. Je vais te détailler les règles que tu as mis en place :

RewriteEngine On #ici tu actives le rewrite.
RewriteCond %{REQUEST_FILENAME} !-d #si ce n'est pas un dossier réel.
RewriteCond %{REQUEST_FILENAME} !-f #si ce n'est pas un fichier réel.
RewriteRule (.*) index.php?router=$1 [L,QSA] #tu rediriges TOUT dans ton index.php et tu passes l'uri en paramètre GET router.

Libre à toi de retravailler ton URI pour n'avoir que les patterns qui te convienne, et rerouter ceux que tu ne gères pas vers une 404. Soit une action classique mais avec le header 404 Not Found.

La fonction suivante devrait tranformer les '/' dupliqués en un seul '/'.

$router = preg_replace('/(\/){2,}/','/',$router);

Bonne chance :)

Evywell
Auteur

Merci de ta réponse Yanis-git
Le soucis c'est que mon url est tout de suite clean. Je m'explique: j'ai fais un var_dump($_GET) sur la première ligne de mon index.php et cela m'a donné

array (size=1)
  'router' => string 'index/13/sdfsdfsdf.txt' (length=22)

pour l'url http://localhost/projet/public/index/13////sdfsdfsdf.txt
alors que je devrais avoir

array (size=1)
  'router' => string 'index/13////sdfsdfsdf.txt' (length=25)

Le problème vient peut-être de mon environement de développement (Debian avec Apache2/PHP7.1/MySQL)
mais je n'ai pas le souvenir d'avoir modifié quelque chose dans les fichiers de configuration.

Merci encore de ta réponse
Bonne journée

PS : Je savais déjà ce que faisait mon .htaccess ^^

peux tu me mettre le retour de :

var_dump($_REQUEST,$_SERVER);
Evywell
Auteur

Voilà pour $_REQUEST

array (size=1)
  'router' => string 'index/13/sdfsdfsdf.txt' (length=22)

Et pour $_SERVER

array (size=34)
  'REDIRECT_STATUS' => string '200' (length=3)
  'HTTP_HOST' => string 'localhost' (length=9)
  'HTTP_CONNECTION' => string 'keep-alive' (length=10)
  'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)
  'HTTP_UPGRADE_INSECURE_REQUESTS' => string '1' (length=1)
  'HTTP_USER_AGENT' => string 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' (length=115)
  'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' (length=74)
  'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate, sdch' (length=19)
  'HTTP_ACCEPT_LANGUAGE' => string 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4' (length=35)
  'PATH' => string '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' (length=60)
  'SERVER_SIGNATURE' => string '<address>Apache/2.4.10 (Debian) Server at local.dev Port 80</address>
' (length=70)
  'SERVER_SOFTWARE' => string 'Apache/2.4.10 (Debian)' (length=22)
  'SERVER_NAME' => string 'local.dev' (length=9)
  'SERVER_ADDR' => string '192.168.56.101' (length=14)
  'SERVER_PORT' => string '80' (length=2)
  'REMOTE_ADDR' => string '192.168.56.1' (length=12)
  'DOCUMENT_ROOT' => string '/media/sf_www' (length=13)
  'REQUEST_SCHEME' => string 'http' (length=4)
  'CONTEXT_PREFIX' => string '' (length=0)
  'CONTEXT_DOCUMENT_ROOT' => string '/media/sf_www' (length=13)
  'SERVER_ADMIN' => string 'webmaster@localhost' (length=19)
  'SCRIPT_FILENAME' => string '/media/sf_www/projet/public/index.php' (length=48)
  'REMOTE_PORT' => string '51055' (length=5)
  'REDIRECT_QUERY_STRING' => string 'router=index/13/sdfsdfsdf.txt' (length=29)
  'REDIRECT_URL' => string '/projet/public/index////13/sdfsdfsdf.txt' (length=51)
  'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
  'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
  'REQUEST_METHOD' => string 'GET' (length=3)
  'QUERY_STRING' => string 'router=index/13/sdfsdfsdf.txt' (length=29)
  'REQUEST_URI' => string '/projet/public/index////13/sdfsdfsdf.txt' (length=51)
  'SCRIPT_NAME' => string '/projet/public/index.php' (length=35)
  'PHP_SELF' => string '/projet/public/index.php' (length=35)
  'REQUEST_TIME_FLOAT' => float 1491990783.665
  'REQUEST_TIME' => int 1491990783
Evywell
Auteur

en faisant un truc comme $router = str_replace('/projet/public/', '', $_SERVER['REQUEST_URI']) ?

Evywell
Auteur

Hm, je ne sais pas si ça un rapport, mais je pense que c'est le serveur web qui gère ça.
Par exemple https://www.grafikart.fr/forum/topics//////26073#p105355 cette url là fonctionne parfaitement alors qu'elle est un peu bizzare.
Ce n'est peut-être pas génant.

Je passe le sujet en résolu.

Merci Yanis-Git pour ton temps passé à m'aider :) Bonne continuation à toi !