【问题标题】:htaccess redirect domain with query string to subdomain with excludeshtaccess 将带有查询字符串的域重定向到带有排除项的子域
【发布时间】:2015-06-13 21:25:34
【问题描述】:

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

这里是旧网址的示例:

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

应该被重定向到:

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

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

这就是我们的 htaccess 的样子:

它确实将网址重定向到子,但不排除新页面并将它们重定向到子域主页(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:/

感谢您的帮助!

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

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

    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]
    

    还要在新浏览器中进行测试以避免旧浏览器缓存。

    【讨论】:

    • 嗨@anubhava,感谢您的帮助!您的代码将所有内容重定向到子域,而不是忽略页面。
    • 它现在似乎可以工作,但我意识到它会重定向新站点的 css 和 js 文件,使其无法正确加载:/ 我试图编写一个规则,仅当 url 包含 php 但不能让它生效。我用目前的代码回答了我的问题
    • 好多了,现在一切都加载了,但主页正在重定向到子域。也许我的其他方法更好?仅当 url 包含 php 时才重定向(我刚刚让它工作!)
    • 做到了 :) 谢谢@anubhava!
    【解决方案2】:

    这是该网站的完整代码。 设置的方式是从名为“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]
    

    以及“main”文件夹中的 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]
    

    我已经在几个浏览器和隐身模式下进行了测试

    谢谢

    【讨论】:

      【解决方案3】:

      我没有排除导致必要的 css 和 js 文件重定向的 url,而是通过创建一个仅影响 php 的 url 的重定向来解决它。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 2015-12-24
        • 1970-01-01
        • 1970-01-01
        • 2014-12-04
        • 2014-07-08
        • 1970-01-01
        相关资源
        最近更新 更多