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.

3

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.)
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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.