How do I trap a bad URL and use it as a paramter?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BeamingBaby
Forum Newbie
Posts: 2
Joined: Tue Oct 06, 2015 5:01 am

How do I trap a bad URL and use it as a paramter?

Post by BeamingBaby »

Hi,

I am a newbie to PHP but have 15 years of asp and Asp.net so UNIX and PHP is a bit out of my comfort zone.
However, I have been charged with the task of providing clients with a personalised url which look like this:

www.somesite.com/sarah

the website "somesite.com" exists but not the folder. The end result will be to extract the folder as a parameter and have a landing page (a bona fide URL) which says: Hi Sarah, welcome to somesite.

As there will be thousands of such urls it isn't practical to create them just to accommodate all possible names.

This may involve some htaccess work, but I am not sure if the parameter (the name, that is) will become lost - again, htaccess is a new experience for me.

Should the above not be workable, I am prepared to look at a workaround but the most important part is that the end user feels that the site has been personalised for them.

Many thanks in anticipation.

G
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I trap a bad URL and use it as a paramter?

Post by Celauran »

Are there any considerations beyond that? You could easily set up an .htaccess rule that converts /sarah to a query string parameter, which the index.php could then read.

Something as simple as
[text]RewriteRule ^(.*)$ index.php?name=$1[/text]
and then you listen for $_GET['name']
BeamingBaby
Forum Newbie
Posts: 2
Joined: Tue Oct 06, 2015 5:01 am

Re: How do I trap a bad URL and use it as a paramter?

Post by BeamingBaby »

Thanks Celauran, but to set up an htaccess catering for 10,000 names or more is impractical.

I have the answer now.

I have set a blanket rule on the htaccess file to redirect all 404 errors to a single page. The tail end of the bad url (say 'Sarah') is extracted. There's a redirect again to the welcome lading page (sometime.com/welcome.php?name="Sarah").

The site is reserved to handling this one marketing drive so the dedicated 404 trapping won't be a problem.

Not wholly elegant, but this works perfectly. I pieced the answer together from about 15 different answers to questions across 15 different sites!

Thanks anyway.

G
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How do I trap a bad URL and use it as a paramter?

Post by pickle »

Since you've already cobbled together something I doubt you'll want to revisit, but here's my input anyway:

There's no need for a 404 error to be triggered. In fact, it might muddy up the logs if you have legitimate 404 errors, and Google's SEO will almost certainly not be happy. @Celauran's rule will work for 10K, 100K, 1000000K names - it's just that one rule for any given name. @Celauran's rule will have index.php doing exactly what you currently have welcome.php doing - just without the ugly 404 errors.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply