1

I was wondering if there was a way to hide the .php file extension with .htaccess.

I've seen quite a few websites do this, for example facebook on most of its pages does not show the .php file extension.

For example I want all my url's to be:

https://mywebsite.com/about

instead of

https://mywebsite.com/about.php

EDIT

Inside my .htaccess file I have the following code already:

RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

Do I just append

RewriteEngine On RewriteRule ^(.*)$ $1.php 

at the end of the file? Or how does it work?

0

7 Answers 7

3

Try this:

This code is currently working in my pc.

<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] </IfModule> 

It will remove extension .php from URL and set remaining URL as it is.

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

Comments

2

your have to active the rewrite_mod

and in your .htaccess

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.*) $1.php [L] 

Comments

1

While this has been answered a few times already, a quick search will show you results like this that should work:

RewriteEngine on RewriteRule ^(.*)$ $1.php 

2 Comments

I edited my post, perhaps you could indicate how can I do it? Since I'm getting an internal error code 500.
you should only need to enable RewriteEngine once, not sure that should cause a 500 error though
1

add the following to the .htaccess file (make sure you have mod_rewrite enabled)

 RewriteEngine on RewriteRule ^(.*)$ $1.php 

Comments

1

Add:

Options Multiviews 

and let mod_negotiation take care of this for you.

Comments

1
RewriteEngine On RewriteRule ^(.*)$ $1.php 

Second line explanation:

^ Start of line ( Capture everything enclosed (start) . Any single character * Zero or more of any character ) Capture everything enclosed (end) $ End of line $1.php Get the captured match and append .php, there's the rewrite! 

Voila!

hello => hello.php home => home.php 

1 Comment

I already have RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Do I just append that code at the end of the line?
0

by the way ,if you are not familiar with .htaccess syntax,

you can try this htaccess rule generator for generate rules

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.