Skip to main content

What is .htaccess file? – How to redirect PHP file?, Example of .htaccess file.

htaccess background image

What is .htaccess file?

hello world!!! .htaccess is a configuration file that use To ran Apache web server software. .htaccess file is it’s very helpful to redirect SSL certificate http to https. 404 not found page error redirection to a web page. In .htassess rewrite code we will fix URL canonization. .htaccess rewrite code we will override the default behavior of Apache server. It’s very helpful to secure a web application from the hackers.

How to use .htaccess file?

Fast open notepad or any other code editor like Notepad++ or Sublime text. Then create a file and save it as a “.htaccess“. Now this file is ready for upload in Apache web server. then go to your Cpanel and upload this file in your route folder. when you upload this file in root folder it will effect all the file under the root folder of your project. but if we do not type any code in the .htaccess file then its not working properly because there is no rewrite code to effect your project.

how to save a .htaccess file

How to hide PHP extension using .htaccess rewrite code?

The very first question is that why we hide the extension of any PHP or HTML file extension in our Browser URL bar. we hide the extension of our file because of SEO friendly URL. If you want to site your file exchange for example dot HTML or dot PHP file extension for your browser URL then use .htaccess extension hide rewrite code. If now you are enter a URL without any extension then it’s open without any problem.

#PHP Extention Hide
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

How to hide PHP extension and trailing slash using .htaccess rewrite code?

The very first question is that what is trailing slash?. Trailing slash means in the end of our browser url bar all a slash(/). In your .htaccess file paste below code then see if now you are enter a URL without any extension then it’s automatically add a slash in end of your url and it’s open without any problem.

#PHP Extention Hide & trailing slash
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]

How to redirect not found page to 404 page using .htaccess redirect code?

not found page

HTTP 404 not found page error means that what you type in browser URL is not found in the server. If you type a URL in URL bar and the URL page is removed or You type address is wrong then it’s show 404 page not found. If your website is showing this type of error then it’s very harmful for your website SEO rank in google. Then the below .htaccess code is very useful for your 404 page error fix-section. Just copy the code and paste in four .htaccess file in your server.

#404 page
ErrorDocument 404 /404.php

How to fix URL Canonicalization issue using .htaccess redirect code?

#URL Canonicalization
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

How to redirect http to https using .htaccess redirect code?

#force ssl
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

How to fix IP Canonicalization issue using .htaccess redirect code?

#BEGIN Redirect IP to domain name
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^123\.45\.678\.901 [NC]
RewriteCond %{HTTP_HOST} ^123\.45\.678\.901
RewriteRule (.*) https://www.exmaple,com/$1 [L,R=301]
ServerSignature Off

How  to enable gzip compression in .htaccess file?

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent

How  to get file from a folder in .htaccess rewrite code?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^$ [NC,OR]
RewriteCond %{HTTP_HOST} ^$
RewriteCond %{REQUEST_URI} !
RewriteRule (.*) /$1 [L]

How  to fix index.php or index.html page URL Canonicalization issue to redirect to website URL only?

RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]


Leave a Reply

Notify of