I have an application with a filenames a bit long.

I would like to remove the name of the file and extension of the php files and only show my domain or server name.
Example: this is what i have:
www.example.com/dir/my_long_file.php

This is what i need.

www.example.com

Dani AI

Generated

Short answer: you can’t make the browser permanently show only the domain for different pages purely from the server side without redirecting or using client-side tricks. The address bar reflects what the browser requested; server-side code can route or rewrite requests, but it can’t silently replace the URL shown to the user for distinct pages.

A practical, maintainable approach is to serve pages through a single entry point (a front controller) and use URL routing so filenames don’t appear (clean URLs). Example: rewrite unknown requests to index.php so you can expose friendly paths instead of long filenames:

# apache: route non-file requests to index.php (keeps the requested URL)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]

If you really want the address bar to show only the domain after a page loads, use the HTML5 History API on the client to replace the path without reloading:

<script>
  // replace the current URL path with root (no reload)
  history.replaceState(null, '', '/');
</script>

Notes and cautions: and correctly showed ways to read server values, but reading the host or request URI does not change what the user sees. ’s session idea can work for preserving state, but it’s awkward for bookmarking and back-button behavior. Hiding the path completely (via frames or registrar “masking”) is strongly discouraged — it breaks SEO, bookmarking, analytics, and often triggers browser/frame security (X-Frame-Options). If you use history.replaceState, ensure your server can serve the same content for the replaced URL (so reloads/bookmarks work), use absolute asset paths or a <base> tag, and provide fallbacks for older browsers.

Recommended: prefer meaningful, short URLs or a front-controller router rather than trying to hide all paths; it’s better for users, search engines, and debugging.

Recommended Answers

All 4 Replies

This code is for localhost:

$site_name  =   $_SERVER['REQUEST_URI'];
$site_name  =   explode('/', $site_name);
echo $site_name[1];

And this is for cloud server:

function get_root_domain(){
    $url    =   strtolower($_SERVER['HTTP_HOST']);
    $url    =   ereg_replace('www\.', '', $url);
    $url    =   parse_url($url);

    return (!empty($url['host'])) ? $url['host'] : $url['path'];
}

echo get_root_domain();

Thank you for the reply.
But the idea is not to get the host name. But to replace the browser information such that in the browser you can read only www.example.com.

Member Avatar for Member #120589

Is this what you need? how about bookmarking certain pages? If all pages share the same url?

This can be done with sessions, but it's a bit awkward. I may be wrong.

$url = $_SERVER['REQUEST_URI'];
$explode_url = explode('/', $site_name);
echo $explode_url[1];

commented: copy of chayassongi's post +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.