Richy Code

Best Blogger tutorials and Tech Tricks website.

Showing posts with label Php. Show all posts

Monday, 5 November 2018

REMOVE .php and .html extension from website url


There are lots of questions that go into RICHY </ > Technical, about how to remove the .php and .html extensions on the website .
One easy way you can do is to edit the .htaccess file on hosting. Editing .htaccess files can be done via cPanel or Plesk, via the file manager and then click edit on the .htaccess file.
Following are the steps:
  1. Remove the .php extension Use the following script to replace the .php extension
    "RewriteCond% {REQUEST_FILENAME}. Php -f"
    this command works to find the .php file
    "RewriteRule ^ (. *) $ 1.php"
    This command works to delete files with the extension .php
  2. Remove the .html extension Use the following script to replace the .html extension
    "RewriteCond% {REQUEST_FILENAME} .html -f"
    this command works to search for .html files
    "RewriteRule ^ (. *) $ $ 1.html"
    This command works to delete files with the extension .html
For additional information, the above script sometimes cannot be used on some CMS such as WordPress or Website Builder like Weebly, because it has its own .htaccess structure or changes in extension from the permalink side.
So the guide to removing the extension .php and .html with .htaccess , hopefully useful.

How to connect to your database using php


<?php
$host = "localhost";
$username = "root";
$password = " ";
$database = "database name";


$conn = mysqli_connect("$hostmysql","$username","$password");

if (!$conn) die ("Connecttion Failed");

mysqli_select_db($database,$conn) or die ("Database Not selected); 


?>

Code Explained

STEP 1: 

$host = "localhost";
$username = "root";
$password = " ";
$database = "database name";

This is the basic details you provide when you are using a localserver like XAMP, WAMP OR MAMP on your computer. The details may changed you have customize your server or when you are using an online server you will be given your server details

NOTE!: If you are using Xamp or Wamp or maybe Mamp the $host name is always localhost and $username is always root leave the $password empty!

STEP 2: 


$conn = mysqli_connect("$hostmysql","$username","$password");


Once you have completed the first step, You now have to create a variable and a fuction that connects to the mysql database using the details above. 

$conn is just a variable i used to connect, (You can name yours anything eg: connect etc). mysqli_connect is the function use to connect to a database.
you the put the name of your variable in the first step in a parentheses or round brackets ( ) if i may say. with this step if there is no errors, The you are connected.

STEP 3: 


if (!$conn) die ("Connecttion Failed");


When coding in php, you must always check for error first. what the above means is that we are telling php that when the $conn variable which is assigned to the mysqli_connect fails, then we use a function called die() which alerts us that "The connection failed"; and also stops the process.


STEP 4:

mysqli_select_db($database,$conn) or die ("Database Not selected); 

What the aboves means that mysqli_select_db( ) function selects the database that you named in STEP 1, but when it fails the die() function alerts that Database is not selected but connection is made. this could be that you type a wrong database name or the database does not exit .


If you got no error message then that means you have sucessfully connect to your mysql database and you can include that connect in your projects.

Conclusion


the ! sign means not, it is used to check for errors and always mostly used in php statements.




If you have any problem in connecting your database or any php issue don't forget to send me a message or leave your question in the comment section. Share this post guys.