Forum Moderators: phranque

Message Too Old, No Replies

Dynamic pages SEO

         

smagdy

7:08 am on May 6, 2005 (gmt 0)

10+ Year Member



Can someone lead me to a simple tutorial/guide for changing the URL of dynamic pages (PHP) so it can be indexed by google at least?

Thanks in advance

jatar_k

11:33 pm on May 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you have Apache then mod rewrite would be the way to go

[webmasterworld.com...]

smagdy

9:05 am on May 7, 2005 (gmt 0)

10+ Year Member



Hi, again.. thanks a lot for the reply and the other sticky mail that I got..

I read and got it but just still one missing connection here, so i will write an example and correct me if i am wrong please..

lets say ive link that was originaly

<a href="bmw.php?cat=$cat&id=$id">BMW</a>

so after applying RewriteRules it would look like

RewriteRule ^/bmw/([0-9]*),([0-9]*)$ /bmw.php?cat=$1&id=$2

-------------------------------------------

I think that was right so far so now my confusion is in the php file how should the link look like and should i create and empty folder called for ex. bmw

and would the link look like this..

<a href="bmw">BMW</a>

thanks

smagdy

5:00 pm on May 9, 2005 (gmt 0)

10+ Year Member



anybody?

jdMorgan

4:20 am on May 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In a nutshell:

  • Link on the page: <a href="bmw-$cat-$id">BMW</a> (use php preg_replace to create this link if you can't change the underlying database)
  • Visitor clicks the link, browser requests http://www.example.com/bmw-32-12
  • Request arrives at your server.
  • Mod_rewrite (assuming it's in httpd.conf) applies the following rule:

    RewriteRule ^/bmw-([0-9]+)-([0-9]+)$ /bmw.php?cat=$1&id=$2 [L]

  • Mod_rewrite modifies the local URL-path to /bmw.php?cat=32&id=12 and activates the bmw.php script.
  • /bmw.php, using the cat and id parameters, produces the correct page with more links like step 1.
  • Cycle repeats.

    If you are interested in SEO aspects, use hyphens, not underscores, and certainly not commas (they are restricted characters and will have to be encoded as '%2C', which is ugly).

    To prevent your 'unfriendly' URLs from being indexed, or to replace those previously indexed, add another rule:


    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /bmw\.php\?cat=([0-9]+)&id=([0-9]+)$
    RewriteRule ^/bmw\.php$ http://www.example.com/bmw-%1-%2 [R=301,L]

    This will 301-redirect any direct requests for the 'unfriendly' dynamic URL to the new 'friendly' static URL.

    It is not necessary to create any directory or file corresponding to /bmw-32-12 -- There is no required one-to-one relationship between URLs and files.

    Jim

  • smagdy

    4:38 am on May 10, 2005 (gmt 0)

    10+ Year Member



    Thanks a lot but this will show
    [example.com...] in the URL

    so how to let it show just
    [example.com...]

    jdMorgan

    2:57 pm on May 10, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Yes, if you want to use mod_rewrite for this application, you need some way to reconstruct *all* of the information in the original (dynamic) URL when looking at only the information in the requested (static) URL.

    For example, you can use the static URL-path /bmw to point to /bmw.php with no problem. But then, where will you get the values to populate the cat=32&id=12 query string? The static URL must contain all information needed to call the dynamic URL. It may be in a different form, but the information must be present.

    If the above example doesn't do what you need to do, then you'll need to make a complete 'map' of all static URLs and their corresponding dynamic URLs, and make sure that the relationship betwen static and dynamic URLs is unique and unambiguous:

    /bmw-sedan-blue -> /bmw.php?cat=32&id=12
    /bmw-sedan-red --> /bmw.php?cat=32&id=14
    /bmw-cycle-blue -> /bmw.php?cat=33&id=12
    /bmw-cycle-red --> /bmw.php?cat=33&id=14

    etc. You can then use RewriteMap to do the translation in httpd.conf or, worst-case, build a list of individual RewriteRules, one for each fixed static-dynamic realtionship, i.e. sedan=cat32, cycle=cat33, blue=id12, red=id14. Obviously, the more organized the relationship is, the fewer rules will be needed.

    Another alternative is to do the parameter translation (e.g. red->id=14) at the php level, since you'll have database access to help you.

    Jim