Home > Software engineering >  How to redirect or 'hide' the .php ending on pages
How to redirect or 'hide' the .php ending on pages

Time:07-09

I had a static HTML website. I am now using some PHP and so my pages have the .php ending. For example www.example.com/about.php

How can I redirect or 'hide' the .php ending so that the above example becomes www.example.com/about ?

I have a .htaccess file for my old website. Code below. But I don't know how to edit it so that it applies to my new .php pages.

RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(. )$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

## hide .html extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/ (. ?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(. ?)/?$ $1.html [L]

The first bit about adding www and turn on https I'd like to keep.

The second bit about hiding .html extension needs to be changed.

The last bit I don't think I ever needed and can be deleted. I don't think I need to rewrite any internal links from /file to /file.php

Many thanks

CodePudding user response:

You can use the same commands that are used for html in your present .htaccess. Simply replace html by php.

Hence,

...
## hide .html extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/ (. ?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(. ?)/?$ $1.html [L]

would become

...
## hide .php extension
# To externally redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/ (. ?)\.php[\s?] [NC]    # html --> php
RewriteRule ^ /%1 [R=301,NE,L]

# To internally rewrite /file to /file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f                 # html --> php
RewriteRule ^(. ?)/?$ $1.php [L]                       # html --> php

in your .htaccess.

Obviously, this will disable the 'functionnality' for .html files.

CodePudding user response:

It sounds like when someone visits www.example.com/about.php you have a file on your server at the root level to handle it called about.php. And if someone visits another page like www.example.com/another-page.php you would have a separate php file saved at the root level called another-page.php to handle that request.

If that's what you're trying to do, then I think you can just add one conditional statement to the htaccess file that rewrites not only .html but also .php to remove the endings, like this:

RewriteCond %{THE_REQUEST} \s/ (. ?)\.php [NC]

But if you are doing what I think you're doing on the backend, you will still need the last part for internal file mapping. The middle part basically says, if someone externally requests www.example.com/about.php send them away with an 301 code telling them to try again at www.example.com/about instead. So browsers will kindly abide by your direction and come back with a second request to www.example.com/about. However, on the back end, your server needs to know what to do with the request to /about. If you want the file called about.php to handle it, you need to tell the server that with the following lines:

# To internally rewrite /about to /about.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. ?)/?$ $1.php[L]

Here is your edited .htaccess file for completenes:

RewriteEngine On

# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(. )$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

## hide .html and .php extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/ (. ?)\.php [NC]
RewriteCond %{THE_REQUEST} \s/ (. ?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. ?)/?$ $1.html [L]

Most php frameworks send all their traffic to one entry point, for example /index.php and work out the routing based on the url. In other words, the frameworks tell the server, don't worry about trying to route requests to the right files, just give them all to me at index.php and I'll figure it out from there. Of course, you need to have logic that determines if the url is valid, and if so grabs the right file and echos it out once found. For example, in that situation, if someone came to your site with a request to something.php your back end framework would send a 301 code saying, "sorry, try again without the php next time".

  • Related