Yaseen Hussain
Yaseen Hussain

Reputation: 62

Get Text after domain name from URL without sending to not found page

I have a domain name say for example : www.example.com I would like to get a dynamic data using PHP that is after this domain name. Example : www.example.com/samsung.

This samsung shall be anything that a user comes from. I want to get this samsung in my PHP. The major problem here is that when ever I open this www.example.com/samsung or www.example.com/vivo page the browser goes to vivo directory and throws a 404 error.

For now I have solved getting the data from this format : www.example.com/?samsung

<?php 
$key = array_search('', $_GET);
echo $key;
?>

But I want to get rid of the ? and have a pure www.example.com/samsung type.

Upvotes: 0

Views: 163

Answers (2)

user1805543
user1805543

Reputation:

This will give you last part of url

$url = $_SERVER['PHP_SELF'];
$url_array = explode('/',$url);
$result = end($url_array);
$Cleaned_url = str_replace("?", "", $result);
echo $Cleaned_url;

UPDATE : Creating Seo url :

.htaccess File

RewriteRule ^([a-zA-Z0-9_-]+)$ your_page.php?p=$1 [L,NC]

In php file when linking to url.

I save urls in database in news_url column

<a href="<?php echo $row["news_url"]; ?>" title="">post title</a>

This setup will give you www.example.com/samsung and solve your 404 notfound problem with right setup. Attetion : Creating seo urls with htaccess requires knowledge just copy paste wont work.

you can search on google : for how to create seo url with htaccess

This examples are working 100%.

Upvotes: 1

Clemens Tolboom
Clemens Tolboom

Reputation: 1962

Use parse_url. See also this answer

Upvotes: 0

Related Questions