0

I am trying to redirect an entire folder (containing hundreds of pages) to the same folder on another domain. The site uses a CMS that uses rewritecond to generate pretty page url's:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

This seems to be conflicting with my redirect. When I use redirect 301 /folder/ http://www.site.invalid/folder/, the pages are redirected to http://www.site.invalid/folder/index.php?q=folder/pagename.html which returns a 404.

Could anyone point me in the right direction?

Nick
  • 5,978
  • 2
  • 27
  • 45
bolvo
  • 333
  • 3
  • 11

2 Answers2

2

Instead of using a 301 like this:

redirect 301 /folder/ http://www.site.invalid/folder/

Try using a 301 like this, and placing it before the other rewrite:

RewriteRule ^/folder/(.*)$ http://www.site.invalid/folder/$1 [R=301,L]

Basically what will happen now is this rule will run first, do the 301 redirect (because it has a R=301 flag), and since it has a last flag (L) then it will stop the server from processing any more rewrites after that.

So your htaccess file would look something like:

RewriteEngine On

RewriteRule ^/folder/(.*)$ http://www.site.invalid/folder/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Nick
  • 5,978
  • 2
  • 27
  • 45
  • Thanks Nick, what i have so far, thanks to your post, is : `code`RewriteEngine on RewriteRule ^(.*)$ http://www.site.invalid/folder/index.php?q=$1 [L]`code` Now the pages get redirected to http://www.site.invalid/folder/index.php?q=pagename which already shows the page, but does not 'prettify' the url. I am not sure why I need to use the rewriterule on the new site, it does not seem to be doing anything and is not working. The solution does not work for another site with content in the root. The redirect goes to http://www.site.invalid/folderindex.php?q=pagename (no slash after folder!) – bolvo Dec 17 '13 at 09:53
  • Your redirect on site 1 should redirect to the pretty URL. Then you should have a rewrite on site 2 to read that URL and send it to the correct page. [Reference: mod_rewrite, URL rewriting and “pretty links” explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained/20566547) might be of some help – Nick Dec 17 '13 at 09:56
  • The redirect does not go to the pretty url, even if I add the rewrite on site 2. I can't understand why... I am already happy that the pages display, but from an SEO point of view I would be a lot happier if the pretty url was displayed. Why doesn't the exact same code work for the redirect on www.site3.tld to www.site.invalid/folder/ ? The pages get redirected to www.site.invalid/folderpagename.html (no slash after folder). – bolvo Dec 17 '13 at 10:21
1

Keep your entire .htaccess like this:

RewriteEngine On

RewriteRule ^(folder/.*)$ http://www.site.invalid/$1 [R=301,NC,NE,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?q=$1 [L,QSA]

In general keep your 301 rules before other catch-all rules.

anubhava
  • 664,788
  • 59
  • 469
  • 547
  • Which URL didn't work for you? Can you try this in a different browser? Also does any .htaccess exist in `/folder/` also? – anubhava Dec 17 '13 at 16:53
  • ok, to be clear: site1 needs to be redirected to site2. htaccess in site1 is now `code`RewriteEngine on RewriteRule ^(.*)$ site.invalid/folder/index.php?q=$1 [L]`code` site2 has `code`RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)$ index.php?q=$1 [L,QSA]`code` adding the above code to it does nothing. – bolvo Dec 17 '13 at 21:56
  • See my answer above. You need to have `http://` in the target URL but you just have `site.invalid/folder/index.php?q=$1 [L]` – anubhava Dec 17 '13 at 22:01
  • to be clear: i have the http, it is this commenting system that snips it out of my comment. – bolvo Dec 18 '13 at 16:06
  • It is quite difficult to read your `.htaccess` code from the comments. Can you post both `.htaccess` files from `site1 and site2` by editing your question. – anubhava Dec 18 '13 at 20:12