WETHINKBLOGS

TECHNOLOG AND BLOGGING ARENA!

Breaking News

Breaking News: Hello did You know that emmy jiya is a Blogger, Thanks for being here.

Tuesday 22 January 2019

What Is .Htaccess File? – How To Redirect PHP File?, Example Of .Htaccess File.

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.
  1. #PHP Extention Hide
  2. RewriteEngine On
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. 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.
  1. #PHP Extention Hide & trailing slash
  2. RewriteEngine On
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteRule ^([^/]+)/$ $1.php
  5. RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
  6. RewriteCond %{REQUEST_FILENAME} !-f
  7. RewriteCond %{REQUEST_FILENAME} !-d
  8. RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
  9. 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.
  1. #404 page
  2. ErrorDocument 404 /404.php

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

  1. #URL Canonicalization
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4. RewriteCond %{HTTP_HOST} ^floxenterprises.com [NC]
  5. RewriteRule ^(.*)$ www.floxenterprises.com/$1 [L,R=301]
  6. </IfModule>

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

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

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

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

How  to enable gzip compression in .htaccess file?

  1. <IfModule mod_deflate.c>
  2. # Compress HTML, CSS, JavaScript, Text, XML and fonts
  3. AddOutputFilterByType DEFLATE application/javascript
  4. AddOutputFilterByType DEFLATE application/rss+xml
  5. AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  6. AddOutputFilterByType DEFLATE application/x-font
  7. AddOutputFilterByType DEFLATE application/x-font-opentype
  8. AddOutputFilterByType DEFLATE application/x-font-otf
  9. AddOutputFilterByType DEFLATE application/x-font-truetype
  10. AddOutputFilterByType DEFLATE application/x-font-ttf
  11. AddOutputFilterByType DEFLATE application/x-javascript
  12. AddOutputFilterByType DEFLATE application/xhtml+xml
  13. AddOutputFilterByType DEFLATE application/xml
  14. AddOutputFilterByType DEFLATE font/opentype
  15. AddOutputFilterByType DEFLATE font/otf
  16. AddOutputFilterByType DEFLATE font/ttf
  17. AddOutputFilterByType DEFLATE image/svg+xml
  18. AddOutputFilterByType DEFLATE image/x-icon
  19. AddOutputFilterByType DEFLATE text/css
  20. AddOutputFilterByType DEFLATE text/html
  21. AddOutputFilterByType DEFLATE text/javascript
  22. AddOutputFilterByType DEFLATE text/plain
  23. AddOutputFilterByType DEFLATE text/xml
  24. # Remove browser bugs (only needed for really old browsers)
  25. BrowserMatch ^Mozilla/4 gzip-only-text/html
  26. BrowserMatch ^Mozilla/4\.0[678] no-gzip
  27. BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  28. Header append Vary User-Agent
  29. </IfModule>

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

  1. RewriteEngine on
  2. RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
  3. RewriteCond %{HTTP_HOST} ^www.example.com$
  4. RewriteCond %{REQUEST_URI} !example.com/
  5. RewriteRule (.*) /example.com/$1 [L]

No comments:

Post a Comment