Reputation: 609
I'm helping someone try to clean up the url's of their website that was built and not totally finished. It uses a custom CMS built off of PHP/MySQL on a VPS host. The original developer kind of went AWOL and one of the final things that they didn't get to was fixing the urls to be more friendly.
The site is for an apartment management company with multiple apartment buildings in PA/NJ:
In the footer of the site are the links to the individual apartment detail pages (note that some of the apartment buildings are named simply after the town they are in). The urls look like:
We'd like to get these to look something like: http://www.yourmetropolitan.com/apartments/pennsylvania/bucks_county/doylestown/doylestown (again, the first doylestown is because that is the name of the town and the second is because it is the name of the apartment building itself)
So as you can see pretty much all url segments exist but we need to rearrange them and hide some of the ugly ID number stuff in there.
Also, there are pages that you can get to from the bread crumbs that group like apartments together such as:
http://www.yourmetropolitan.com/welcome/feature/Montgomery_County/Narberth/17/Pennsylvania/Narberth/27 which gives you the two buildings in the town Narberth. We'd want to clean this up as well.
I guess what I'm asking is if this looks like something to clean up with mod_rewrite OR change the code that generated these long ugly urls to begin with?
Everything I've seen about mod_rewrite deals with urls that have query strings like example.php/user?id=100 but not moving around url segments like this.
If it is mod_rewrite, if any super nice person wants to get me started with some Regex, I'd really appreciate it. However, I know this is asking for the obligatory "what have you tried?" reply. Which I totally understand.
Upvotes: 0
Views: 66
Reputation: 2556
This really looks like two questions:
Q1) Can I use mod_rewrite to help me clean up these URLs?
Q2) If so, how would I go about doing this?
To which I would answer:
A1) Yes, mod_rewrite can help. But to redesign your URLs you will also need to modify your application code. Although mod_rewrite is perfect for mapping/dispatching incoming HTTP requests with a nice, logical structure onto an existing (though perhaps unlovely) set of accessible resources/endpoints, it will do nothing to help your application emit URLs with a nice logical structure in its HTTP responses. You will either have to modify the code which renders the outbound URLs inside your application, or add your own "outbound URL rewriting layer" to the application (which mangles your HTTP responses to conform to your new scheme just before they are sent).
A2) Begin by formally defining both your current/existing URL schema and the new one which you want to implement. I like to draw pictures, some people like to use text, but my advice is to have something logical worked out before you dive into the regexes and the arcane details of mod_rewrite. This will make it easier to ask coherent questions if & when you run into trouble, and help you avoid wasting time on a poorly thought out scheme (as with any Software Development project). There are lots of mod_rewrite examples available published online, and the module documentation is well maintained.
Upvotes: 1