How to hide .php

I’ve tried everything possible to hide my .php extension to make my website look more professional.

I’ve tried this:

Force HTTPS

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

Hide .php extension more effectively

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Redirect /index.php to /

RewriteRule ^index.php$ / [R=301,L]

This:

DefaultType application/x-httpd-php

And many other settings, but nothing works!

Is there some kind of default setting in Infinity Free that prevents me from hiding the .php extension?

This should do the job

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

now link your php files without the .php and it should work
ie /someFile.php becomes /someFile

5 Likes

I did it. It doesn´t work. I had another take, getting help from IA, but it doesnt work too. This is my last config, it didn´t work either. I think Infinity Free has some limitation:

# Activar mod_rewrite
RewriteEngine On

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

# Ocultar extensión .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)$ $1.php [L]

# Redirigir todo a router.php si no es un archivo o directorio existente
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ router.php [L]

# Redirigir /index.php a /
RewriteRule ^index\.php$ / [R=301,L]

Its not a limitation of infinity free. As people have done this before and it’s worked.

you’re not trying to remove the ?i=1 in you? because if your .htaccess does that it’ll be blocked (I cant see anything that would do this, but thought I’d check)

I used this thread, and the example works for my site:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]

please note this doens’t make the php extention automatically vanish. It just means that when writing out the link, you don’t need to include the “.php”

For me, that meant a full rewrite of my site to update the links haha. But it was worth it

Remember that caching can cause problems too. you may need to clear your cache, or use an incognito session on your browser to see the change.

5 Likes

If you want to “hide the .php extension”, I assume you want to two things:

  • If the user navigates to a page like http://example.com/contact, it should load the file contact.php.
  • If the user navigates to http://example.com/contact.php, the user should be redirected http://example.com/contact.

Regarding the first item, this code does exactly that:

Regarding the second item, you don’t have any code for that yet. But I would expect that something like this should do the trick:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)\.php$ $1 [R=301,L]

If this doesn’t do what you want it to, can you please elaborate on what you mean by “hide the .php extension”? Please explain what should happen on your website if a visitor accesses a certain URL.

5 Likes

I want to be able to do exactly this:
If the user navigates to a page like http://example.com/contact, it should load the file contact.php.
If the user navigates to http://example.com/contact.php, the user should be redirected http://example.com/contact.

That sounds like exactly what I said.

So the rest of my message applies. Please read it.

7 Likes

just borrowing that for my own site… thank you

6 Likes