Page 1 of 1

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

Posted: Tue Oct 06, 2015 5:16 am
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

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

Posted: Tue Oct 06, 2015 6:27 am
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']

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

Posted: Tue Oct 06, 2015 7:07 am
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

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

Posted: Thu Oct 08, 2015 5:19 pm
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.