0

I would like to delete extensions like .php, .html, etc from my url and this is what I use:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

It works but I don't know if this is the best way to do this and I also don't understand how it works, could someone explain it?

The second thing I would like to realise is to make the URL more beautiful by doing something like this:

this is my URL: http://domain.com/portfolio/project.php?id=1 this is what I would like to see: http://domain.com/portfolio/project/1

Thanks in advance

2
  • Is this .htaccess located in portfolio/ directory? Commented Sep 9, 2016 at 10:06
  • no in the root (domain.com), so at the same level as the portfolio folder Commented Sep 9, 2016 at 10:08

1 Answer 1

2

You can use these rules in site root .htaccess:

RewriteEngine On

# rewrite /portfolio/project/25 to /portfolio/project.php?id=25
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(portfolio/[\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA,NC]

# rewrite /portfolio/project to /portfolio/project.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# rewrite /portfolio/file to /portfolio/file.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I will test if this works but can you also explain how this works?
I've added comments before each rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.