homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

How to rewrite these urls
rewriting urls

 10:26 am on Aug 16, 2008 (gmt 0)


I'm trying to rewrite some URLs that do not exist anymore to the new URLs.

The old URLs are:


The new URL needs to be:


I'd appreciate any help with this. I'm not so great at regex.


[edited by: jdMorgan at 12:14 am (utc) on Aug. 18, 2008]
[edit reason] example.com [/edit]



 12:10 pm on Aug 16, 2008 (gmt 0)

Are you sure you want a rewrite?

I assume you need a 301 redirect.

They are two similar but different things.


 12:31 pm on Aug 17, 2008 (gmt 0)

You don't really need to use mod_rewrite, you can use mod_alias:

RedirectMatch 301 ^/?property\.php\?city=(.*) http://www.example.com/list/index.php?location=$1

[edited by: jdMorgan at 12:14 am (utc) on Aug. 18, 2008]
[edit reason] example.com [/edit]


 12:41 pm on Aug 17, 2008 (gmt 0)

How does that fix up the "state" parameter?

It doesn't, so he will need to use a RewriteRule.


My initial question was *not* about Mod_Rewrite vs. Mod_Alias.

It was about whether the poster wanted an internal rewrite or an external redirect.

He asked for a rewrite, but gave two URLs as an example.


 7:07 pm on Aug 17, 2008 (gmt 0)

Yes, I do believe what I need is a 301 redirect.
Essentially I've re-organized my site, moved things around.

Of course my old URLs are the ones indexed by SEs. I want to salvage the traffic that comes from those, so they do not get a 404.

Sorry, I should have been more clear in my question.

BTW, thanks for the quick responses, that was pretty awesome.


 10:28 pm on Aug 17, 2008 (gmt 0)

A lot of people mix up the terminology here, so getting the question right is important. :-)


I believe you'll need something like this:

RewriteCond %{QUERY_STRING} ^city=(.*)&state=NY&type=xyz
RewriteRule ^property\.php http
://www.example.com/list/index.php?location=%1&type=xyz [R=301,L]

There will be a better way to optimise that, as .* is greedy. Perhaps use ([^&]+) or something.


I also don't like "index.php" in a URL so I might actually go for this instead:

RewriteCond %{QUERY_STRING} ^city=([^&]+)&state=NY&type=xyz
RewriteRule ^property\.php http
://www.example.com/list/?location=%1&type=xyz [R=301,L]


In addition I would add something like:

RewriteCond %{THE_REQUEST} list/index\.php
RewriteRule ^list/index\.php http://www.example.com/list/ [R=301,L]

to always remove the "index.php" from the URLs. The original query string should be re-appended on the end.

On my own sites I wouldn't need that specific rule as I always redirect for all common index file names, to remove the index filename and preserve the rest of the URL.

[edited by: jdMorgan at 12:15 am (utc) on Aug. 18, 2008]
[edit reason] example.com [/edit]


 11:04 pm on Aug 17, 2008 (gmt 0)

There is one really big unasked question here.

I don't understand why you don't jump to completely static-looking URLs like:


To do that you would have:

1. A redirect from the old dynamic URL to the new static URL with [R=301,L]

2. A rewrite from the static-looking URL to the internal dynamic filepath with [L]

3. The static-looking URLs within the links in the content of your site.

[edited by: jdMorgan at 12:16 am (utc) on Aug. 18, 2008]
[edit reason] example.com [/edit]


 12:12 am on Aug 18, 2008 (gmt 0)

Hi g1smd,
Yes, this is really what I'd like to accomplish with the URLs.


And I like your idea to remove the index.php from the URLs to. Just unnecessary.

How ever there are other Query String parameters that I would need to carry over as well. Like


I should have included this in my original post too, I just wanted to keep it simple and to the point.

[edited by: jdMorgan at 12:17 am (utc) on Aug. 18, 2008]
[edit reason] example.com [/edit]


 9:09 am on Aug 18, 2008 (gmt 0)

If the parameters have a consistent format, or for multiple formats can be identified by the presence of a particular element, then you can use RewriteCond to test those. You might end up with multiple rules one each for the various formats, but it can be done.

Before you start, you have to be clear what all the dynamic internal formats look like, and then choose static-looking URL formats that will be easy to test to discover which internal format they need to be rewritten to.

Write them all down. Mull it over for a day or two. At this point I fall back on the "Measure twice. Cut once." school of thought.


 5:01 pm on Aug 26, 2008 (gmt 0)

Hi g1smd,

I've been working on my site, getting the URLs to just how I want them. This is a real estate listings site, so there can be a lot of parameters in the URL.

Currently, I have the search form set to GET method, I could change this to POST if needed.

?location=San+Jose (city or zip code)(state is hard coded in script)
&ltype=for+rent (for sale or for rent)
&ptype=apartment (home, apartment, townhome, several others)
&page=4 (page number of the resutls)
&q=patio&pricemin=&pricemax=2400&bedsmin=2&bathsmin=2 (these can stay as QS variables, didn't see need the to put in the URL)


I figured those last parameters could stay as query string variables, instead of rewriting them in the URL, but
Location, Ltype, Ptype and Page would be good to have in the URL.

What do you think?

I also have some URLs with out all the parameters above...


These would be nice to have as:

I appreciate your help. I thought it would be best to get my new URLs setup correctly, then worry about 301 redirecting old ones to the new.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved