search

Home  >  Q&A  >  body text

How do I remove the file extension in the HTML HEAD metadata of a canonical URL?

I have a website that asks for files without file extensions. For example, the URL http://www.example.com/foo.php should simply access http://www.example.com/foo. < /p>

But please forgive me as I am new to PHP but I would also like the

This will display the full URL, including the file extension.

Is there anyone with more PHP knowledge than me who can modify the above line of code to exclude the file extension?

Thanks in advance!

P粉752826008P粉752826008445 days ago534

reply all(1)I'll reply

  • P粉493313067

    P粉4933130672023-09-15 15:27:48

    You can try to use str_replace to remove the extension

    $string_with_extension = $_SERVER['REQUEST_SCHEME'] . '://' .$_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
    
    $extension = ".php";
    
    $new_string = str_replace($extension,"",$string_with_extension);

    Simply put, you just replace the extension with "" or nothing will get you the URL you want.

    <link rel="canonical" href="<?php echo $new_string; ?>">

    reply
    0
  • Cancelreply