Forum Topic: Hide PHP extension name

Forum: .htaccess Forum : Redirecting • Posted by David • Updated:

Hi,

I try to hide the .PHP in my url:

www.mywebsite.com/page2.php

in to:

www.mywebsite.com/page2

Can you please show the complete htaccess code!?

Thanks in advance!

David

8 Replies to “Hide PHP extension name”

Jeff Starr
Posted by Jeff Starr

Hi David,

Is there an actual resource (e.g., web page) available at

http://www.mywebsite.com/page2

Usually, PHP is required to redirect to dynamically generated content.

Posted by David •

Thanks Jeff!

test url:

http://www.smooth1.yourweblab.nl

Jeff Starr
Posted by Jeff Starr

Okay so when I visit

http://www.smooth1.yourweblab.nl/page2

it doesn't exist (404).

I guess what I'm saying is that, in order to redirect a URL via .htaccess, there must be an actual (or virtual) resource at the destination URL. So if you redirect, say, this:

http://www.smooth1.yourweblab.nl/page.php

to this:

http://www.smooth1.yourweblab.nl/page

then /page must be an actual resource. This can be done with PHP and .htaccess, as is the case with WordPress Permalinks. But .htaccess alone can only redirect - it can't create the destination resource.

I hope this makes sense, let me know if I can elaborate on anything, glad to do so.

Posted by David •

Hi Jeff,

I managed to hide the PHP extension. But do I leave both pages on the sever for example contact.php and contact?

This is added to the htaccess file:

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

It works without the use of PHP, do I have to add PHP on all pages and what PHP?

If so on what page do I have to add a canonical tag and how does it have to look? Because Google will find both pages on the server.

How does my XML site map has to look? Does it have to show both files with and without the extension?

Can you please elaborate and explain the whole story?

How do I change www.mydomain/index.php in only www.mydomain.com?

Thanks!

David

Jeff Starr
Posted by Jeff Starr

The "whole story" depends largely on your server, configuration, domain, file structure, development strategy, SEO goals, and many other variables. I can, however, try elaborate further on the strategic side of implementation, if it will be useful.

Basically start with a single file and get that worked out. First try it with the file named contact. Then try it with the file named contact.php. Then compare your test results and determine which type is going to work best for you and your site. For example, if you can get one or the other working with sitemaps, SEO, and whatever, then it makes sense to use that format over the other.

Once you have it figured out for a single PHP file, you are all set to apply the logic to your entire set of files. So for example if you go with both files, contact and contact.php, then make sure to include both types (with and without the extension) for all files. Then test rigorously to make sure that everything is dialed in.

Posted by David •

Thanks Jeff!

test site: www.take200.yourweblab.nl

htaccess code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

And what about how to hide index.php and change it in a other URL like HOME?

On my test site I have "Home" showing index instead of index.php and Webplan instead of Webplan.php, but when I change the URL in the navigation from index to something else like Home it doesn't work how is this possible?

My Question is also: I have the 2 pages index.php and index and de 2 pages webplan.php and webplan, where do I have to put the canonical tag to show the search engines the proper page to index?

Regards,

David

Posted by David •

I managed to get the /home visible when I click Home instead of showing /index.php.

Jeff Starr
Posted by Jeff Starr

"And what about how to hide index.php and change it in a other URL like HOME?"

So to do this you can use either mod_alias or mod_rewrite, depending on your preference, for example I would do something like this:

RedirectMatch 301 ^/index.php http://example.com/whatever/

Then replace the example URL to whatever URL makes sense, e.g., home.

"My Question is also: I have the 2 pages index.php and index and de 2 pages webplan.php and webplan, where do I have to put the canonical tag to show the search engines the proper page to index?"

Are you referring to the rel canonical tag? If so, I would read from the source:

https://support.google.com/webmasters/answer/139066?hl=en

I think normally rel canonical tags are included in the document head, but there may be other factors involved here.