Thursday 22 April 2021

HTACCESS + PHP | Forcefully Add Ending Slash (/) In URL On Rewrite Rule

 


You can download the whole folder from:
https://unpossiblepog.com/codes/PHP+HTACCESS/HTACCESS-Forcefully-Add-Slash-At-The-End-Of-URL

.htaccess
RewriteEngine on

RewriteRule ^([^\.]+)/$ $1.php [NC,L]

ErrorDocument 404 http://%{HTTP_HOST}/<project-folder>/error.php?ref=%{HTTP_HOST}%{REQUEST_URI}

-----------------------------------------
error.php

<?php

if(isset($_GET['ref'])) {

$path_name=$_GET['ref'];

$file_name_array=explode("?", $path_name);

//echo $this_path."<br/>";

//echo $file_name_array[0]."<br/>";

if(isset($_SERVER['HTTPS'])){$http_https = "https://";}else{$http_https = "http://";}

$file_name=$http_https.$file_name_array[0];

header("location: ".$file_name."/");

  //echo $file_name."<br/>";

}

?>

-----------------------------------------
index.php

<h1>This is INDEX file.
</h1>
-----------------------------------------

about.php

<h1>This is About page.</h1>
-----------------------------------------
hello.php

<h1>This is hello page.</h1>

=======================


Subscribe my Channel:- http://www.youtube.com/user/SanketRooney?sub_confirmation=1

Facebook Page:- https://www.facebook.com/UnpossibleNS

Twitter Account:- https://twitter.com/UnpossiblePOG

Blog :- https://unpossiblepog.blogspot.in/

-------------------------------

Hey guys, we shall create a code to force browser to add slash at the end of URL using HTACCESS and PHP.


Make sure to use INCOGNITO mode while implementing this because sometimes the HTACCESS code remains in a cookies which won’t help to run the updated code of HTACCESS.


Currently in folder, we have some PHP files, each file contains some text.


And we have the REWRITE RULE in HTACCESS file which will allow you to run the file without using the “.php” extension. And the rule contains an ending slash in first parameter.

So if you enter the URL without extension and end with slash, it will run that page.

But if you remove the ending slash, it will give you a 404 error. The best solution is to forcefully add ending slash in URL using the code.

So in HTACCESS, write the following code, code link is given in the description.

Notice that this file is inside the “tutorial” folder which is inside HTDOCS folder of xampp.

You have to add the URL accordingly.

So I have already created an error.php page which will forcefully add slash. Code is given in the description.

Our strategy is to send the current URL as a request parameter when error occurs.

And as you can see, it redirects and ends with slash.

So that is it, thanks for watching. Like share and subscribe.


Tuesday 6 April 2021

Clone Private Github Repository Files Inside Live Website With CPANEL, GIT VERSION CONTROL & SSH


-------------------------------

Putty:- https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

SSH Keygen command:-
ssh-keygen -t rsa -b 4096 -C "username@servername"


Subscribe my Channel:- http://www.youtube.com/user/SanketRooney?sub_confirmation=1


Facebook Page:- https://www.facebook.com/UnpossibleNS


Twitter Account:- https://twitter.com/UnpossiblePOG


Blog :- https://unpossiblepog.blogspot.in/

-------------------------------

In this video, we shall make a connection between private github repository and live cpanel.

First you must have a domain with cpanel and a github account.

I purchased a domain+cpanel from godaddy. You can choose your own hosting providers but make sure that the SSH access is enabled. For godaddy users, here is the path to find and enable SSH access.

Next, open github, login into your account, or create account if you don’t have one.

Click on plus icon which is before your profile pic, click on new repository.

Give any random name.

You can make repository as private from here or if you forgot to make it private, you can make it afterward.

Just go to settings tab, scroll down, you will see, “Change repository visibility”, click on a button in front of it and can click on private and save it.

Now, login into your domain’s cpanel.
Then open PUTTY. If you don’t have it, download link is given in the description. Install it.

Now copy the IP address from cpanel dashboard.
Paste it in HOST NAME textbox, make sure that the SSH radio button is selected, then click on OPEN.

It will open a command prompt.

Type the username which you use for login into cpanel.

Hit enter.

Now type the cpanel’s login password.
When you type the password, it will not show anything on command prompt, but whatever you are typing is getting stored in the background.

Once you logged in, you will see something like this.

Once connected, you will have to type the command to create public and private key.

It will be given in the description.

In double quote, type username@servername.

Keep the filename & passphrase empty.

Now, go to cpanel and SSH ACCESS : MANAGE SSH KEYS.

You will see the newly created keys there.

Click on MANAGE of the public key, AUTHORIZE it.

Let’s jump to github, open your repository and click on settings tab, then DEPLOY KEYS. Then ADD DEPLOY KEY.

Give any key name.

For public key, go to cpanel, and click on VIEW/DOWNLOAD link in front of public key.

Copy it from start to end and paste it in github’s key textarea.
Check the ALLOW WRITE ACCESS, Of course we already can download files from github to cpanel by default. But giving the WRITE ACCESS means, you can upload changes from cpanel to github.

Once done, open the putty command prompt, type the following command to check if the domain can be connected to github or not.

Now we are successfully authenticated.

From github, lets create a new file. Type anything there. Give it any random name.
Scroll down and commit the change.

You will have a new file in your repository.

Go to cpanel dashboard, click on git version control, then CREATE.

Now open github, click on code, then click on SSH tab, copy the text from the textbox.

Paste it in CLONE URL textbox of cpanel.

For repository path, you can not add those folders which not empty.

For example if I add public_html folder which already has so many files and try to clone it, it will give me an error.

So you can either empty the public_html folder or create a new empty folder which is what I am going to do.

Now you can create a clone of private github repository.

You will be redirected to GIT repository listing. Click on the MANAGE button.

And click on PULL OR DEPLOY.

You will see that those UPDATE AND DEPLOY buttons are disabled.

First, lets check if our original file is copied to our website’s directory by going to the FILE MANAGER.

The file is there and it contains the text that we typed.

Let’s do some changes in github file and try to reflect it to our live site.

To synchronize or pull the changes, lets go to CPANEL’s dashboard : Git version control : MANAGE : PULL OR DEPLOY and click on UPDATE FROM REMOTE.

Lets again go to file manager, and view the file.
Changes are reflected there.

So that is it, thanks for watching. Like share and subscribe.