How to change link in core php

Let's give an example

I have a link: - localhost/project/search?uni=1

I want to change it this way: - localhost/project/search/uni-of-uk

I am not using a framework, its core is PHP

+3


source to share


4 answers


Like this ... based on the little information you gave ....

$link = 'localhost/project/search?uni=1';
$link = str_replace('?uni=1', '/uni-of-uk', $link);

      

NEW: in PHP you can change the title to change the url.



if($_GET['uni']==1)
{   header('Location: localhost/project/search/uni-of-uk');
    exit;
}

      

The important thing is that you DO NOT EXIT until you call the header function. Therefore, before any exit, you must place this code on top of your script.

0


source


What you are looking for is called URL Rewriting

.

You can achieve this using mod_rewrite

(assuming you are using apache).



You might want to look into this mod_rewrite article

0


source


If you want to change

localhost/project/search?uni=1

      

to

localhost/project/search/uni-of-uk/1

      

You can achieve this by creating .htaccess in your root folder and adding something like this to it,

RewriteEngine on
RewriteRule ^/?search/uni-of-uk/([0-9]+)$ /search?uni=$1

      

Another method is PHP routing , large CMS sites follow this method as stated here .

0


source


This might help you:

Write below code in .htaccess file.

RewriteRule ^ search / ([^ /] +) search.php? uni = $ 1 [NC]

Now you can get the "uni" value using $ _REQUEST ['uni'] or $ _GET ['uni'] in your php file.

0


source







All Articles