views:

93

answers:

1

Hi! I'm trying to match a a bunch of redirects for my website with basically moved to a different folder on the server. I need to make http://www.site.com/index.php?page=anypage go to http://www.site.com/newfolder/index.php?page=anypage. The thing is http://www.site.com/index.php and http://www.site.com/index.php?page=home should remain untouched. How can I accomplish this?

I was trying the following in the .htaccess file, but I am affraid to make a mistake. I really don't know how to test this, either.

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^/index.php?page=(.*)$ http://www.site.com/newfolder/index.php?page=$1 [R=302,NC]
RewriteRule ^/index.php?page=home http://www.site.com/index.php?page=home [R=302,NC,L]

Now I figured that this is temporary, so I should know ho to reverse it! The next week, the links will have to redirect again to the root server. Also, what should I do to re-establish the normal redirection??

+1  A: 

If I've followed your scenario correctly, you want something like this:

RewriteEngine On

RewriteCond %{QUERY_STRING} !=""
RewriteCond %{QUERY_STRING} !page=home
RewriteRule ^index.php /newfolder/index.php [R,L]

As far as testing goes, I prefer to try rules out on a local test server. If you have full control over the server (as is the case locally), there are some mod_rewrite directives that help you log what's going on, and that can be helpful in debugging. The module documentation has more information about this.

Edit: When you want to switch back, modify the RewriteRule above like so:

RewriteRule ^newfolder/index\.php /index.php [R,L]
Tim Stone
It's working perfectly! Thanks a lot. Should I add the R=302 to the last line? It's a temporary redirect for Google and search engines. The next week, the links will have to redirect again to the root server.Also, what should I do to re-establish the normal redirection??
Landitus
No problem, glad to hear it's working. It's not necessary as the default value for `R` happens to be 302. When you want to switch back, you just have to change the rule around. I'll edit the answer to include what that would look like.
Tim Stone
Great Tim, you've been very helpful!
Landitus
Is there a way to only redirect Google and other search engines? Cause there's going to be a new version of the site on the root, so both need to be accesible, but the root version is the canonical and gets the SEO benefits.
Landitus
You can `RewriteCond` based on any HTTP headers sent, so it might be possible to do it that way. I'm not sure what criteria would be useful for that, though, since it's not anything I've tried to do before. [The module documentation](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond) has information on what types of variables are available to you if you can think of something that you can check to differentiate in this case.
Tim Stone