Htaccess php no extension

Remove .php extension from the URL [htaccess]

I love PHP, but my boss does not. Once, he was not happy to see the project coded in PHP by seeing the .php extension on the URL, citing the reason as it is an old language and many new languages like NodeJS have emerged which should’ve been used to code the project! Though all my explanation and convincing went in vain! I decided to remove .php extension from the URL. Of course, removing the .php extension does not really prevent people from knowing the language that was used to build an application. But if your boss is dumb just like many out there, then one solution was to hide. Said that, from a security perspective, it is good to do so. Other benefits of using an extension-less URL are as follows:

  • URLs look cleaner
  • They are easy to type
  • It’s easy to remember
  • Make URLs more SEO keyword friendly (Though no direct SEO benefit)
  • You can change the underlying technologies
  • Can hide technologies behind it as explained earlier.
Читайте также:  Примеры скриптов java скриптов

To remove the file extension we need to make changes in the .htaccess file.

Check your website’s root folder for the file “.htaccess”. If you don’t have one, create it.

Remove php extension

Add the following code to your .htaccess file and save it.

Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / # To externally redirect /dir/foo.php to /dir/foo RewriteCond % !POST RewriteCond % ^[A-Z]\s([^.]+)\.php [NC] RewriteRule ^ %1 [R=302,L,NE] ## To internally redirect /dir/foo to /dir/foo.php RewriteCond %.php -f [NC] RewriteRule ^ %.php [L]

If you have an AJAX request to PHP files with the POST method, then the above code might have an issue and such requests stop working. To bypass POST requests, look for the line RewriteCond % !POST

How to exclude a specific file/URL from the .htaccess Rewrite Rule?

If you want to keep the extension to certain files as some of them might be getting data from POST methods and sometimes errors can occur while redirecting those pages. To exclude a file from removing the extension, just add the following line to your .htaccess code.

Exclude specific file/URL

Exclude specific sub-directory

Источник

How to remove .php, .html, .htm extensions with .htaccess

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 % !-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:

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

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 % !-f RewriteRule ^([^/]+)/$ $1.php RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php RewriteCond % !-f RewriteCond % !-d RewriteCond % !(\.[a-zA-Z0-9]|/)$ 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 % !-d RewriteCond % !-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 to remove .php, .html, .htm extensions with .htaccess

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 % !-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:

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

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 % !-f RewriteRule ^([^/]+)/$ $1.php RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php RewriteCond % !-f RewriteCond % !-d RewriteCond % !(\.[a-zA-Z0-9]|/)$ 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 % !-d RewriteCond % !-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:

Источник

Removing File Extensions from URLs using .htaccess (.php, .html)

When it comes to creating a website, one of the most important things to consider is the user experience. This includes not only the design and functionality of the website but also the structure of the URLs.

A clean and simple URL structure can make it easier for users to understand the organization of the website and improve the overall navigation. Additionally, it can also help with search engine optimization (SEO) by making it easier for search engines to crawl and index your pages.

The benefits of hiding file extensions:

  • Back-end technology is hidden from end users. But it’s still not hard to identify the technology for experts.
  • The best pros of this are that we can easily change backend technology without affecting the SEO of the pages.
  • Read more about .htaccess here

One way to improve the structure of your URLs is by removing the file extensions, such as .php and .html. This can make your URLs appear cleaner and more professional, and can also help to hide the technology used to build the website.

The process of removing file extensions from URLs can be done using the .htaccess file. The .htaccess file is a configuration file used by the Apache web server and can be used to make changes to the server’s behavior, including URL rewriting.

Removing .php Extension from URL

For example, You need to change the URL from http://example.com/about.php to http://example.com/about.

Create a new `.htaccess` file or edit an existing one. This file should be placed in the root directory of your website. Add the following code to the `.htaccess` file:

Источник

Оцените статью