SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)

    What/How would you do with this rewrite?

    I changed domain names. I have 1000+ pages. All the paths in the new domain have change significantly. Currently I just used this blanket rule in the htaccess to rewrite all files to the new domain root. RewriteRule (.*) http://www.newdomain/ [R=301,L]. (is that a good way to do that first of all??). But my question really is... I feel lazy doing this. But to write out a 1000 rewrite rules and painstakingly work out the new paths seems like a lot of room for error and it would take me forever. What would YOU do in this case?

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,857
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    A couple years ago I changed the URL structure on a site from one based on an ID number (mysite.com/index.php?id=12) to one using a search engine friendly slug (mysite.com/slug-with-keywords). It was probably somewhere in the neighborhood of 4,000 pages affected. What I did was create a database table mapping the old ID numbers to the new slug, and I created the table by writing a PHP script to do it. It didn't take that long to write the script to create the map table. Then I wrote an htaccess rule to check if the page request matched the ID number pattern and if so, I rewrote it to a PHP file which queried the database to retrieve the new URL then issued a 301 redirect.

    It did not take that long to do and was an effective solution. It took much less time than it would have taken to write out thousands of htaccess redirects which is probably not a good idea anyway.

  3. #3
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cheesedude View Post
    A couple years ago I changed the URL structure on a site from one based on an ID number (mysite.com/index.php?id=12) to one using a search engine friendly slug (mysite.com/slug-with-keywords). It was probably somewhere in the neighborhood of 4,000 pages affected. What I did was create a database table mapping the old ID numbers to the new slug, and I created the table by writing a PHP script to do it. It didn't take that long to write the script to create the map table. Then I wrote an htaccess rule to check if the page request matched the ID number pattern and if so, I rewrote it to a PHP file which queried the database to retrieve the new URL then issued a 301 redirect.

    It did not take that long to do and was an effective solution. It took much less time than it would have taken to write out thousands of htaccess redirects which is probably not a good idea anyway.
    Ya from what little I know I see that's the way to do it. That must be so empowering to just be able to write all that yourself. Unfortunately I am not empowered with that stuff.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,680
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Eric,

    First, you've learned the lesson about planning a site change (I hope).

    Second, what you need is a RewriteMap. Unfortunately, you need access to the httpd.conf to configure that as it can easily bring an entire server down for a simple typo (syntax error).

    Third, what cd described is what I've been calling "A Poor Man's RewriteMap," a PHP script which can read a database or just a flat file (essentially, just a table with two columns) which should only be used for 404 situations. Kudos to cd for working that out! While the read-a-file isn't a difficult thing for PHP to do, I'd just create an array in the 404.php script to do that rather than read a separate file. Of course, the redirection would entail TWO header("location:...") lines, one for a 301 status code and the second for the redirection.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Eric,

    First, you've learned the lesson about planning a site change (I hope).

    Second, what you need is a RewriteMap. Unfortunately, you need access to the httpd.conf to configure that as it can easily bring an entire server down for a simple typo (syntax error).

    Third, what cd described is what I've been calling "A Poor Man's RewriteMap," a PHP script which can read a database or just a flat file (essentially, just a table with two columns) which should only be used for 404 situations. Kudos to cd for working that out! While the read-a-file isn't a difficult thing for PHP to do, I'd just create an array in the 404.php script to do that rather than read a separate file. Of course, the redirection would entail TWO header("location:...") lines, one for a 301 status code and the second for the redirection.

    Regards,

    DK
    Thanks guys for the info. No I new what was coming. Changing the paths was worth it.

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    If it is PHP and alot of rewrites, you probably want database not flat file. Reason being PHP will have to parse that flat file on every request whereas it can just query the database which will likely be largely more efficent.

    If you can get into a persistent environment then flat files become more interesting as you can do something like hydrate a dictionary and keep it in RAM and take advantage of very fast lookups that way.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •