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


htaccess redirect domain with query string to subdomain with excludes

我们已经将我们的网站移动到子域,而在主域上,我们有一个带有新url的新网站。我们试图做的是将旧url重定向到子域,同时排除新url。

以下是旧url的示例:

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

应重定向到:

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

而新网站的url(不包含php)和主页不应该受到影响。

这就是我们的htaccess的样子:

它确实将url重定向到sub,但不排除新页面并将其重定向到子域主页(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]

希望有人能帮助我们做到这一点,我们现在的gwt中有很多404:/

谢谢你的帮助!

修复一些正则表达式并重新排序规则:

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]

还要在新浏览器中测试这一点,以避免旧的浏览器缓存。

以下是该网站的完整代码。设置方式是从名为"main"的文件夹调用新站点,而从根目录调用旧站点。所以我们实际上有2个htaccess。

以下是根htaccess代码:

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]

以及"主"文件夹中的htaccess:

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]

我在一些浏览器上测试过,在隐姓埋名模式下也测试过

感谢

而不是排除导致必要的css&js文件进行重定向,我通过创建一个重定向来解决这个问题,该重定向只影响php的url。

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