How to Remove .Php .Html Via .Htaccess?

5 minutes read

To remove the file extensions .php and .html from appearing in URLs using the .htaccess file, you can use rewrite rules. Add the following lines of code to your .htaccess file:


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


RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ $1.html


These rules will remove the extensions .php and .html from the URLs of your website. Just make sure to update any internal links and references within your website to reflect the changes.

Best Web Hosting Providers of November 2024

1
Vultr

Rating is 5 out of 5

Vultr

  • Ultra-fast Intel Core Processors
  • Great Uptime and Support
  • High Performance and Cheap Cloud Dedicated Servers
2
Digital Ocean

Rating is 4.9 out of 5

Digital Ocean

  • Professional hosting starting at $5 per month
  • Remarkable Performance
3
AWS

Rating is 4.8 out of 5

AWS

4
Cloudways

Rating is 4.7 out of 5

Cloudways


What is the Deny from All directive in .htaccess?

The "Deny from All" directive in .htaccess is used to block access to a specific directory or file by denying all IP addresses access to it. This directive essentially tells the server to deny any requests to access the specified resource. This is a security measure that can be used to protect sensitive files or directories from unauthorized access.


What is the difference between Redirect and Rewrite in .htaccess?

Redirect and Rewrite are both commonly used directives in .htaccess files to control the behavior of web server requests. However, they serve different purposes:

  1. Redirect: Redirect is used to send the user's browser to a different URL. It tells the server to issue a temporary or permanent redirect response (HTTP status codes 301, 302, 307, or 308) to the client, indicating that the requested resource has moved to a different location. This is useful when you want to redirect traffic from an old URL to a new URL, or when you want to handle different variations of URLs (such as redirecting non-www URLs to www URLs).


Example: Redirect 301 /old-page.html http://www.example.com/new-page.html

  1. Rewrite: Rewrite is used to internally rewrite a URL before it is processed by the server. It allows you to create more user-friendly URLs, mask file extensions, or rewrite dynamic URLs into static URLs. This is done using regular expressions to match incoming URLs and then rewriting them to a different location.


Example: RewriteEngine on RewriteRule ^category/([a-zA-Z0-9-]+)$ category.php?cat=$1 [L]


In summary, Redirect is used to send the user's browser to a different URL, while Rewrite is used to internally rewrite URLs on the server before processing them.


How to redirect non-www to www using .htaccess?

To redirect non-www to www using .htaccess, you can add the following code to your .htaccess file:

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


This code will check if the domain does not start with "www." and then redirect the visitor to the www version of the domain. Make sure to replace "example.com" with your actual domain name in the code.


Save the changes to your .htaccess file and upload it to the root directory of your website. The redirect should now be in effect, redirecting all non-www URLs to the www version.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To get back to index.html in .htaccess, you can use the RewriteRule directive to redirect requests back to the index.html file. You can add the following code to your .htaccess file:RewriteEngine on RewriteCond %{REQUEST_URI} !^/index.html RewriteRule ^ /index...
To add HTTPS via the .htaccess file, you need to first ensure that your website has an SSL certificate installed. Once that is done, you can redirect all HTTP traffic to HTTPS by editing your .htaccess file.In the .htaccess file, you can add the following code...
In order to set the timezone in PHP via .htaccess, you can use the SetEnv directive to set the timezone variable. For example, to set the timezone to UTC, you can add the following line to your .htaccess file: SetEnv TZ UTCThis will set the timezone to UTC for...
To hide the .php extension with .htaccess, you can use mod_rewrite in your .htaccess file. This can be achieved by creating rewrite rules that will internally redirect requests for URLs without the .php extension to the corresponding PHP files with the extensi...
To remove "?q=" from the URL using .htaccess, you can use the following code:RewriteCond %{QUERY_STRING} ^q=(.)$ RewriteRule ^(.)$ /$1? [R=301,L]This code checks if the query string contains "q=" in the URL and removes it by redirecting to the ...
To redirect from HTTPS to HTTP, you need to modify your website's .htaccess file or configure your server settings. Here's how you can do it:Open the .htaccess file: Connect to your web server using FTP or file manager. Locate the root directory of you...