Forum Moderators: coopster

Message Too Old, No Replies

Header redirects with variable content

redirect page

         

Brandie

4:21 pm on Nov 25, 2011 (gmt 0)

10+ Year Member



Please help this newbie!

I have a script that generates dynamic listings for a few hundred rental properties. How can I generate a page with a more SEO-friendly URL (http://mydomain.com/rosecottage.htm rather than mydomain.com/listing.php?P=134) - accepting that my .htaccess file will redirect any response back to the correct listing.php script?

I understand that I can call the 'new' page using header('Location: [mydomain.com...] but can't my head around how to insert the "content" built in listing.php into the new page. Or, is there some other way of sending the page with the new content, but under a different name.

Thanks!

rocknbil

5:50 pm on Nov 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Brandie, you might back up a step and consider a couple things.

First (you probably already know) this will require some sort of automation with mod_rewrite. You put the URL's in your page output and rewrite them to your script, like

^/12345/first-street/san-diego$ /listing.php [L,NC]

(That's a poor example, but to give you the gist)

So your links will all output as

<a href="/12345/first-street/san-diego/">12345 First Street San Diego</a>

How do you "get" to those links? You could try some preg_replace() wizardry to take the title and add dashes, convert it all to lower case, but then you have to be cautions of users who may mess up your plan by adding stuff like quotes, commas, and sales pitch verbiage like

12345 First Street San Diego "CUTEST COTTAGE EVER", LOOK!

Honestly, I've done that, it's more trouble than it's worth. It's so much easier to add another field to your database specifically for the URL that you can control by only allowing letters and numbers into it, and substituting spaces for dashes. This makes it easier to maintain, easier to control, and you get far fewer surprises.

That's really "half the battle" and once you arrive there, you won't have to use hard-coded rewrites like my example, you should arrive at some sort of one-liner scheme - for example, if you decide there is no real "listings" directory, and any request for a "listings" directory will write to your script, you could do sometihng like

RewriteRule ^listings(.*) /listings.php?uri=$1 [L,NC]

Then in listings.php, you'd parse out the url parameter to search your DB for a matching url field.

Two things (among many, these are just on top) are important to remember about this approach.

The first is that your browser will still "think" you're in the listing directory. So any relative references to images or css won't work.

[domain root]
/listing-images
listings.php

<img src="listing-images/123456.jpg" alt="1234 First Street San Diego">

... because not only is there no "listings" directory, there certainly is no "listings/listing-images" directory.

This is really easily fixed (and is a common problem many encounter) by using a root relative path for all images, CSS, flash, Javascript - everything.

<img src="/listing-images/123456.jpg" alt="12345 First Street San Diego">

<link rel="stylesheet" type="text/css" href="/mycss.css">

The leading slash means "start at the domain root and follow this path" and will work from everywhere in your file system.

The second is if you already have some of these listings indexed, it's really important to understand and include 301 redirects in your .htaccess for the old query string URL's as soon as you start doing this, and to make sure the old ones are no longer accessible. Often called "duplicate content penalty," it's not really a "penalty" per se, it's that the search engines see two versions of the same page and divide the strength between them.

Brandie

10:14 am on Nov 29, 2011 (gmt 0)

10+ Year Member



Thanks, rocknbil

I really appreciate the trouble you've taken and need to go through your advice with a fine toothcomb in a nice quiet environment if this tired old brain is to taken it in and fully understand it. So, I know what I will be doing this evening.

Although this answers my overall question, there is a much simpler, smaller part of the question that got lost in the overall picture....

How can you send variables, or other content, to a page that your scripts calls via a header('Location:http://.....') command? I appreciate that I could use a session variable, but is there a neater solution?

For example, lets assume that my script validates POST or GET input and I want to send an error page - but, for various reasons, I want to send a totally different page rather than return the same page with an error message. However, I want to include a personalised message compiled by the initial script. So, my script compiles the error message $error and then uses header() to call message.php - but message.php needs to display the content of $error.

Thanks, in anticipation.

rocknbil

5:08 pm on Nov 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I don't use this approach, but you'd do it pretty much like you say:

$error = check_data(); // Your data checking function that returns null for no errors or or a string, if errors

if ($error) {
header("location:error-handler.php?e=" . urlencode($error));
}

.. and error-handler.php accepts only one parameter, "e" (in get, as in $_GET['e'])

Generally if someone submits data and there's an error, it's a matter of good practice to bring them back to that page with the form fields intact, otherwise they are left using their back button, which often flushes out the fields - or - if it's say, a two step process, they get the "data must be resubmitted" message from the browser. That's why I seldom, if ever, use a location header for errors.