SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2010
    0 Post(s)
    0 Thread(s)

    Do I have to rewrite all my site's hrefs after I use mod_rewrite to re-write URLs?


    I have successfully used mod_rewrite to rewrite all the URLs (which originally contained query strings) for a PHP based site I have been working on. They looked good and everything, but then I realized I would have to go back through the site's HTML content and re-write a lot of the hrefs in the links so that they would point to the new SEO/human readable URLs.

    This aspect of it (re-writing hrefs) was a pain, and I really don't feel like doing it on the new site I'm working on, especially because Google have been saying they can handle dynamic URLs nowadays and because I know I will have to go through the site and re-write all those hrefs. Also, now I need to go back to my old site and make some structural changes, and I am going to have to spend a lot time re-writing hrefs inside the <a> tags just because I will make some changes to the .htaccess file. This seems too awkward.

    Is there a solution for this whereby I can perhaps have a PHP class that parses the mod_rewrite rules in the .htaccess file and then uses them to publish the most up-to-date URLs inside the hrefs of the <a> tags? All I would then have to do is write 'includes' or function calls in place of the hrefs in my 'views', instead of hard coding hrefs. OR is there a simpler way to do this with a built in system in PHP (or library?) or Apache server?

    I would really appreciate any help thanks.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    The Netherlands
    153 Post(s)
    2 Thread(s)
    I don't think there is a PHP script that will do this for you. At least I've never come across any. There is an alternative where you can let Apache rewrite the old URLs to the new one, but this will hog a lot of resources, and make you a lose a little bit of link juice every time; overall just not a really good idea.

    So indeed what's left is to rewrite all URLs yourself. Maybe you could get your editor to find and replace using regular expressions to make it easier? I know for example NetBeans has this option; you could even do it across several files at once.

    It's a bit awkward to do when you first worked with the dynamic URLs, and then change them to "pretty" ones. For future projects, it's better to start using the "pretty" URLs from the get go. Or use a URL manager in PHP itself, like the big frameworks have; they just redirect every request to one php file, and let that file figure out what to do, based on how the URLs are configured in PHP. There are also functions to create URLs based on the same configuration, which makes it very easy to change URLs; just change the configuration and the functions will return the new URLs, and they will work too. Add some 301 redirect if necessary, and you're done.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    19 Post(s)
    3 Thread(s)

    That question had been asked before ... and it's included in my signature's tutorial along with the advice NOT to abuse a server like this. There is a trick (or two) to it as you must endeavor not to create loopy code (old format to new format to old format ... and prevent continuing this loop ... as I'm sure you've discovered).


    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


Posting Permissions

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