Jump to content

Recommended Posts

Hi,

 

I've been looking at how I can redirect a bunch of variable directories but I'm only finding examples where the directories being redirected are hard-coded. What I want to do is 301 redirect a whole bunch of these:

 

1) http://example.com/foo/file.php?id=bar

 

To this

 

2) http://example.com/foo/file/bar/

 

1 is my current real directory structure, which I'm already successfully mod_rewrite'ing to 2, which  hits my new, simplified file structure:

 

3) http://example.com/file.php?var=foo&id=bar

 

RewriteRule (.*)/file/(.*)/ file.php?var=$1&id=$2

 

This allows me to not have to maintain a ton of virtually identical directories.

 

I'm just looking for a way to redirect any traffic I get to the old structure (1), where I don't know which directory "foo" might be, to it's corresponding path 2.

 

...reading this post I suspect I may have explained it really unclearly :(

Basically if I get traffic to the old structure:

 

http://example.com/foo/file.php?id=bar

 

I want to 301 redirect it using htaccess to

 

http://example.com/foo/file/bar/

 

...heh I just realized that I might be in the wrong forum. I posted here because this is going to work in concert with my rewrite:

 

RewriteRule (.*)/file/(.*)/ file.php?var=$1&id=$2

 

Which allows me to emulate my old directory structure with one file.

 

 

Thanks very much for your help on this, Corbin...

 

I tried that rule, but it looks like you have it where the name of the php file is variable, but that's not the case. I get a 404 when i try the rule you suggested and hit /foo/file.php?id=bar

 

The only file that exists is  /file.php , but that doesn't change names. I used to have a ton of directories each with a file.php within it:

 

/foo/file.php

 

Now I want a) a string like /file.php?directory=foo&id=bar to look like /foo/file/bar/ and b) make sure that any requests for /foo/file.php?id=bar is redirected to the new fake /foo/file/bar/

 

Can this be done with rewrite alone? i was assuming I had to do a rewrite for a, and a redirect for b.

 

 

Well, that was still getting me a 404, but it pointed me in the right direction... I did a little more research and now I'm so close I can taste it except that this is redirecting to

 

/directory/file/3451/?id=3451

 

what I want is just /directory/file/3451/

 

If I can just get rid of the querystring, I'm set!

 

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$

RewriteRule ^([^/]+)/file\.php$ http://example.com/$1/file/%1/ [R=302,L]

 

Figured it out, just needed a question mark at the end of the redirect url to nuke the query string.

 

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$

RewriteRule ^([^/]+)/file\.php$ http://example.com/$1/file/%1/? [R=302,L]

 

Thanks for you help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.