1

I have a php project using a directory structure and I want to create pretty urls. Basically I want to remove ".php" extension of each file and delete path stuff between localhost and the filename.

Example:

transform this => localhost/myproject/views/pages/login/login.php

into this => localhost/myproject/login

I know I have to configure .htaccess mod_rewrite but I don't know how exactly to be honest.

FranSto
  • 39
  • 7

2 Answers2

0

Try and add this to your .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

It should basically remove all the .php extensions from being showed.

Martin
  • 1,849
  • 1
  • 11
  • 17
  • That wasn't really the question though. (Also you shouldn't copy and paste such snippets around without documenting a source/origin link. A little late now for this one, of course.) – mario May 27 '18 at 18:46
  • Thanks but it doesn't work at all. It remove .php extensions but it doesn't delete de middle path. Furthermore the application does not work correctly. – FranSto May 27 '18 at 20:44
0

Okey, I've solved it. I have a index.php file in the root folder so any url will redirect to that file adding RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] in .htaccess using a query string with the view name I want to show. Then I had to rewrite all links to look like "/myproject/name_of_the_view" inside of the project

FranSto
  • 39
  • 7