0

I'm trying to simultaneously do a 301 redirect and imporve my url structure by stripping the get variables.

I recently updated my website and Google has some old pages cached that have since moved. The structure looks like this

wwww.domain.com/locations.asp?zip=10001 <---OLD URL
wwww.domain.com/locations/?zip=10001 <---NEW URL

Right now I'm redirecting the old page to the new using the following line in my .htaccess file:

Redirect 301 /solar_panel_systems_zip.asp /zip-code/

The above works fine but I'd like the URL to be as follows:

wwww.domain.com/locations/zip/10001

I came across this post .htaccess rewrite GET variables and tried this rule but no luck :/

RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [NC]

I'm assuming this is because I'm 301 redirecting and doing a rewrite?

Community
  • 1
  • 1
user1647347
  • 507
  • 1
  • 8
  • 23

1 Answers1

0

You want something like this in your htaccess file:

RewriteRule ^locations/zip/([0-9]+)$ /locations/?zip=$1 [L,QSA]

This will make it so when someone requests http://www.domain.com/locations/zip/10001, they will get served the contents of /locations.php?zip=10001.

To redirect the old URLs to the new ones, you will need to also add:

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /locations(\.asp)?/?\?zip=([0-9]+)&?([^\ ]*)
RewriteRule ^ /locations/zip/%3?%4 [L,R=301]

So when someone tries to go to http://www.domain.com/locations.asp?zip=10001 or http://www.domain.com/locations/?zip=10001, they get redirected to http://www.domain.com/locations/zip/10001.

tim.baker
  • 3,109
  • 6
  • 27
  • 51
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • the 2nd block worked great but the first block is giving me a bit of trouble. Perhaps its because my file isn't locations.php its /locations/?zip= ??? (wordpress page) – user1647347 Sep 14 '13 at 20:10
  • To clarify right now I get redirected to -> http://www.domain.com/locations/zip/10001 but that page isn't found because it's not serving up the php file – user1647347 Sep 14 '13 at 20:16
  • @user1647347 Just change the target of the first rule to `/locations/` then – Jon Lin Sep 14 '13 at 20:18
  • I think thie problem is with "RewriteRule ^locations/zip/([0-9]+)$ /locations/?zip=$1 [L,QSA]" It's hard because wordpress will show the page regardless but I keep landing at http://www.domain.com/locations/zip/10001 but the 10001 data is not showing up. When I visit http://www.domain.com/locations/?zip=10001 the data does show up – user1647347 Sep 14 '13 at 20:27
  • @user1647347 if this is wordpress trying to figure out what to load, it's going to look at the `REQUEST_URI` and see something it doesn't recognize (e.g. `/locations/zip/10001`). You'll have to either use wordpress to handle the redirects or you can try internally proxying by adding a `P` to the flags: `[L,QSA,P]` – Jon Lin Sep 14 '13 at 20:44