htaccess将带有查询字符串的域重定向到带有排除的子域

We have moved our site to a subdomain while on the main domain we have a new site with new url's. What we are trying to do is having the old url's redirect to the subdomain while the excluding the new url's.

here's examples of the old url's:

http://www.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0

should be redirected to:

http://sub.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0

While the new site url's (do not contain php) and the homepage should not be effected.

This is what our htaccess looks like:

It does redirect the url's to the sub, but does not exclude the new pages and redirect them to the subdomain home (sub.domain.com)

RewriteEngine on    
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$  http://www.%{HTTP_HOST}/$1 [R=301,L]   

RewriteCond %{REQUEST_URI} !contact(|$)
RewriteCond %{REQUEST_URI} !about(|$)
RewriteCond %{REQUEST_URI}  index\.php
RewriteRule ^(.*)$ http://sub.domain.com [R=301,L]

Hopefully someone can help us get this right, we get many 404 in our gwt now :/

Thanks for any help !

Fix some regex and reorder your rules:

RewriteEngine on    
RewriteBase /

RewriteCond %{THE_REQUEST} !/(contact|about) [NC]
RewriteCond %{THE_REQUEST} !\.(jpe?g|gif|bmp|png|tiff|css|js) [NC]
RewriteRule ^(.+)$ http://sub.domain.com/$1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Also test this in a new browser to avoid old browser cache.

Here's the full code for the site. The way things are set up is that the new site is being called from a folder named "main" while the old site is being called from the root directory. So we actually have 2 htaccess.

Here's the root htaccess code:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^/([^.]+)\.php
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} www.domain.com$
RewriteCond %{REQUEST_URI} !^/main
RewriteRule ^(.*)$ /main/$1 [L]     

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteRule ^admin$ index.php?dir=app_misc&page=login [L]
RewriteRule ^admin/$ index.php?dir=app_misc&page=login [L]

RewriteRule ^sitemap.xml$ sitemap.php [L]
RewriteRule ^robots.txt$ robots.php [L]

RewriteRule ^adv$ adv/index.php [L]
RewriteRule ^adv/$ adv/index.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(([0-9]+)_([^/]+)|([0-9]+))$
RewriteRule (.*) /404.php?a=%{REQUEST_URI} [L]

and the htaccess in the "main" folder:

RewriteEngine on    
RewriteBase /

RewriteCond %{REQUEST_URI} !(contact|about) [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

I've tested on few browsers and in incognito mode as well

Thanks

Instead of excluding urls which caused necessary css & js files to redirect as well, I solved it by creating a redirect that effect only url's with php.

RewriteCond %{THE_REQUEST} ([^.]+)\.php [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]