Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

639
Views
How to check if a given URL string ends with "/index.*" or simply "/" and remove it, in PHP?

I'm trying to check for /index.[ php | htm | html | asp ], etc. (basically any wildcard suffix)

or simply "/"

to convert a canonical URL such as http://www.example.com/index.php or http://www.example.com/ to just http://www.example.com so I can just use one consistent PHP variable in my canonicalmeta tag throughout all the pages on my site.

I dont want the script to effect URLs that are NOT homepages such as http://www.example.com/page.php

REGEX preferred, but not necessary.

I'm working from the variable $current_Webpage_Complete_URL_Address in "domain_info.php" from this script: http://www.perfecterrordocs.com

I want to use just one PHP variable, formed from modifying $current_Webpage_Complete_URL_Address, if that makes any sense.

Please ask for further clarification, if neccassary.

Edit: Also, I want the newly formed variable to be named $current_Webpage_Canonical_URL_Address

Edit(2): I just ran into another problem. Even when I do find a match with preg_match how do I remove that particular ending sub-string?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Final Answer (Works Perfectly!!):

$current_Webpage_Canonical_URL_Address = preg_replace('/((\\index)\.[a-z]+)$/', '', $current_Webpage_Complete_URL_Address);

$current_Webpage_Canonical_URL_Address = preg_replace('/\/$/', '', $current_Webpage_Canonical_URL_Address);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!