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.
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.
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).
This topic is now closed. New replies are no longer allowed.