Helmet Posted February 15, 2009 Share Posted February 15, 2009 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 Quote Link to comment Share on other sites More sharing options...
corbin Posted February 15, 2009 Share Posted February 15, 2009 Wait.... I'm confused. You want to rewrite 1 to 2? Or you want to rewrite 2 to three? Quote Link to comment Share on other sites More sharing options...
Helmet Posted February 15, 2009 Author Share Posted February 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted February 15, 2009 Share Posted February 15, 2009 RewriteRule ^([^/]+)/(.*?).php\?id=([^&]+) /$1/$2/$3/ [L] Is all I can think of, and you will want to put it before the other rewrite (or it will make a loop). Quote Link to comment Share on other sites More sharing options...
Helmet Posted February 15, 2009 Author Share Posted February 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted February 16, 2009 Share Posted February 16, 2009 Oh, sorry it can't be done with a rewrite, but the rewrite can turn into a redirect. Don't know why I didn't put that in there earlier. Try: RewriteRule ^([^/]+)/file\.php\?id=([^&]+) /$1/$2/$3/ [L,R=301] Quote Link to comment Share on other sites More sharing options...
Helmet Posted February 16, 2009 Author Share Posted February 16, 2009 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] Quote Link to comment Share on other sites More sharing options...
Helmet Posted February 16, 2009 Author Share Posted February 16, 2009 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! Quote Link to comment Share on other sites More sharing options...
corbin Posted February 16, 2009 Share Posted February 16, 2009 You figured it out but no problem ;p. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.