How to remove file extension from url in php

First, verify that the mod_rewrite module is installed. Then, be careful to understand how it works, many people get it backwards.

You don't hide URLs or extensions. What you do is create a NEW URL that directs to the old one, for example

The URL to put on your web site will be yoursite.example/play?m=asdf

or better yet

yoursite.example/asdf

Even though the directory asdf doesn't exist. Then with mod_rewrite installed you put this in .htaccess. Basically it says, if the requested URL is NOT a file and is NOT a directory, direct it to my script:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[.*]$ /play.php [L]

Almost done - now you just have to write some stuff into your PHP script to parse out the new URL. You want to do this so that the OLD ones work too - what you do is maintain a system by which the variable is always exactly the same OR create a database table that correlates the "SEO friendly URL" with the product id. An example might be

/Some-Cool-Video [which equals product ID asdf]

The advantage to this? Search engines will index the keywords "Some Cool Video." asdf? Who's going to search for that?

I can't give you specifics of how to program this, but take the query string, strip off the end

yoursite.example/Some-Cool-Video

turns into "asdf"

Then set the m variable to m=asdf.

So both URLs will still go to the same product

yoursite.example/play.php?m=asdf
yoursite.example/Some-Cool-Video

mod_rewrite can do lots of other important stuff too, Google for it and get it activated on your server [it's probably already installed.]

I recently wanted to remove the extensions from my website, in order to make the URLs more user and search engine friendly. I stumbled across tutorials on how to remove the .php extension from a PHP page. What about the .html? I wanted to remove those as well! In this tutorial I’ll show you how to do that easily, by editing the .htaccess file.

What is an .htaccess file

An .htaccess file is a simple ASCII file that you create with a text editor like Notepad or TextEdit. The file lets the server know what configuration changes to make on a per-directory basis.

Please note that .htaccess is the full name of the file. It isn’t file.htaccess, it’s simply .htaccess.

.htaccess files affect the directory in which they are placed in and all children [sub-directories]. For example if there is one .htaccess file located in your root directory of yoursite.com, it would affect yoursite.com/content/, yoursite.com/content/images/, and so on…

It is important to remember that this can be bypassed. If you don’t want certain .htaccess commands to affect a specific directory, place a new .htaccess file within the directory you don’t want to be affected with the changes, and remove the specific command[s] from the new file.

Features

With an .htaccess file you can:

  • Redirect the user to different page
  • Password protect a specific directory
  • Block users by IP
  • Preventing hot-linking of your images
  • Rewrite URLs
  • Specify your own Error Documents

In this tutorial we’ll be focusing only on rewriting URLs.

Removing Extensions

To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:

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

If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to change the last line from the code above, to match the filename:

RewriteRule ^[[^\.]+]$ $1.html [NC,L]

That’s it! You can now link pages inside the HTML document without needing to add the extension of the page. For example:

wallpaper

Adding a trailing slash at the end

I received many requests asking how to add a trailing slash at the end, for example: yoursite.com/page/

Ignore the first snippet and insert the code below. The first four lines deal with the removal of the extension and the following, with the addition of the trailing slash and redirecting.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[[^/]+]/$ $1.php
RewriteRule ^[[^/]+]/[[^/]+]/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ![\.[a-zA-Z0-9]{1,5}|/]$
RewriteRule [.*]$ /$1/ [R=301,L]

Link to the HTML or PHP file the same way as shown above. Don’t forget to change the code if you want it applied to an HTML file instead of PHP.

Some people asked how you can remove the extension from both HTML and PHP files. I don’t have a solution for that. But, you could just change the extension of your HTML file from .html or .htm to .php and add the code for removing the .php extension.

Conclusion

For those who are not so experienced with .htaccess files there is an online tool for creating them. It’s useful for novice users to get started, and easy to use.

Updates

Attention GoDaddy users: In order to remove the extensions you need to enable MultiViews before. The code should look like this:

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[[^\.]+]$ $1.php [NC,L]

If you’re worried that search engines might index these pages as duplicate content, add a meta tag in the of your HTML file:


How do I hide PHP extension in URL?

By setting expose_php to off in your php. ini file, you reduce the amount of information available to them. For this to work effectively, you must rename your PHP files with the above extensions.

How do I remove .html from URL?

html extension can be easily removed by editing the . htaccess file.

Chủ Đề