0

I want to rewrite my URL's, each url looks like below :

http://www.mywebsite.com/projet-in.php?idProjet=57

And each project got a special name given by database.

My question here is : Is this possible to rewrite my url's like that ? :

http://www.mywebsite.com/ProjectName
  • Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Croises Dec 20 '16 at 19:14

1 Answers1

1

if the project name is unique, yes it is possible : search your project by name instead of searching by id

use this rewrite rule :

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file**
RewriteRule ^(.*)$ projet-in.php?name=$1&%{QUERY_STRING} [NC,L]

this way, when the user goes to http://foo.fr/projectName apache will theat the request as http://foo.fr/projet-in.php?name=projectName so you will get the project in GET parameters in PHP

your new php code will be

<?php
$db = new Database();

$project = $db->findByProjectName($_GET['name']);
ᴄʀᴏᴢᴇᴛ
  • 2,702
  • 20
  • 40
  • this way, the url http://foo.fr/projectName will become http://foo.fr/projet-in.php?name=projectName Well I want the first URL, I wont use the 2nd one – DERET Pierre-Yves Dec 20 '16 at 15:35
  • when the user go to `foo.fr/projectName`, apache will rewrite the url to `foo.fr/projet-in.php?name=projectName` in php you will get the project name in the GET parameters – ᴄʀᴏᴢᴇᴛ Dec 20 '16 at 15:37