【问题标题】:Redirect|Forward from old URL to new URL重定向|从旧 URL 转发到新 URL
【发布时间】:2015-08-23 21:34:13
【问题描述】:

我有一个路由和一个以这种方式操作 URL 的方法:

http://domain.com/combivent?email=WGA1cUtKZi4

现在这个网址已经变成了这个:

http://domain.com/?email=WGA1cUtKZi4

但旧电子邮件仍然保留旧电子邮件。我需要将每个 URL 从旧方式重定向到新方式。例如,如果我称之为http://domain.com/combivent?email=WGA1cUtKZi4,那么我应该重定向到http://domain.com/?email=WGA1cUtKZi4如何在 Symfony2 中实现这一点?我可以在路由中使用任何类型的通配符,还是需要保留这两种方法并在内部处理?这可以在.htaccess 级别完成吗?有什么建议吗?

这就是.htaccess 的样子:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Remove combivent word from the URL when call a brand page
    RewriteRule ^combivent?email=([^/]+)$  /?email=$1 [L,QSA,R=301]

    RewriteRule .? %{ENV:BASE}/app.php [L]    
</IfModule>

此外,domain.com 是共享三个域的 VirtualHost 的别名。将 VH 定义视为:

ServerName maindomain.com
ServerAlias domain.com
ServerAlias domain1.com

更新

以下是完整的.htaccess 定义:

DirectoryIndex app.php
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Remove combivent word from the URL when call a brand page
    RewriteCond %{QUERY_STRING} ^email=[^&]+ [NC]
    RewriteRule ^combivent/?$ / [L,R=301]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /app.php/
    </IfModule>
</IfModule>

【问题讨论】:

    标签: php .htaccess symfony redirect url-routing


    【解决方案1】:

    您可以将旧路由重定向到路由中的新路由,而不是深入研究.htaccess。

    new_route:
        path: /
        defaults: ...
    
    old_route:
        path: /combivent
        defaults:
            _controller: FrameworkBundle:Redirect:redirect
            route: new_route
            permanent: true
    

    当您使用注释时,您只需要命名您的新路由或找出生成的名称并在您的“old_route”定义中使用它。

    已根据您的实际设置进行了更新

    PDOneBundle/Resources/config/routing.yml

    pdone:
        resource: "@PDOneBundle/Controller/"
        type:     annotation
        prefix:   /
    
    # add this
    combivent:
        path: /combivent
        defaults:
            _controller: FrameworkBundle:Redirect:redirect
            route: brand
            permanent: true
    

    PDOneBundle\Controller\BrandPageController

    class BrandPageController extends Controller
    {
        /**
         * @param Request $request
         *
         * @return array
         * @Route("/combivent", name="combivent") <!-- remove this
         * @Route("/", name="brand")
         * @Method("GET")
         * @Template()
         */
        public function indexAction(Request $request)
        {
            ...
        }
    }
    

    【讨论】:

    • 我是用Annotations来做路由的,可以一样实现吗?怎么样?
    • 你可以把它放在你的app/config/routing.yml 中,我假设你已经添加了你的基本注释“import”。或者,您可以将其放在一个特殊的 redirect.yml 文件中,然后您可以从您的 routing.yml 作为资源链接到该文件。
    • 我建议使用@Qoop 的建议。如果您使用 Symfony 的框架来执行此操作,您将不必担心将来网站是否从 apache 迁移到 nginx 或其他 http 服务器。
    • @Qoop this 是我当前的项目设置,您能否根据此来源编辑您的答案?我的路由仍然不太好,我根本没有关注你
    【解决方案2】:

    只需将以下规则添加到您的 .htaccess 文件中:

    RewriteEngine On
    RewriteRule ^combivent?email=([^/]+)$  /?email=$1 [L,QSA,R=301]
    

    编辑:您需要将其添加到上方以下行:

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
    

    【讨论】:

    • 不工作。我已在 Symfony 2.7 项目中添加到 /web/.htaccess 文件
    • 抱歉,来晚了。我刚刚更新了答案。
    • 查看我添加的主要帖子 .htaccess 信息。那样也行不通,也许我在整个mod_rewrite定义的错误地方写了这个
    • @ReynierPM 是的,你需要在它上面的规则之前添加它。 RewriteRule .? %{ENV:BASE}/app.php [L] 正在阻止任何进一步的重写,因为它满足所有模式,并且具有 L 标志。
    • 我需要重启 Apache 服务还是别的什么?我如何确保正在调用规则?还是不行
    【解决方案3】:

    这条规则有问题:

    # Remove combivent word from the URL when call a brand page
    RewriteRule ^combivent?email=([^/]+)$  /?email=$1 [L,QSA,R=301]
    

    因为您无法使用 RewriteRule 匹配 QUERY_STRING。请改用RewriteCond:

    # Remove combivent word from the URL when call a brand page
    RewriteCond %{QUERY_STRING} ^email=[^&]+ [NC]
    RewriteRule (?:^|/)combivent/?$ / [L,R=301]
    

    QUERY_STRING 将自动传送到 nee 目标。

    完整的.htaccess:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    # Remove combivent word from the URL when call a brand page
    RewriteCond %{QUERY_STRING} ^email=[^&]+ [NC]
    RewriteRule (?:^|/)combivent/?$ / [L,R=302,NC]
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]
    
    RewriteRule .? %{ENV:BASE}/app.php [L]
    

    还要确保在测试之前清除浏览器缓存。

    【讨论】:

    • 你的意思是 RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L] RewriteCond %{QUERY_STRING} ^email=[^&amp;]+ [NC] RewriteRule ^combivent/?$ / [L,R=301] 或 RewriteCond %{QUERY_STRING} ^email=[^&amp;]+ [NC] RewriteRule ^combivent/?$ / [L,R=301] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L] 都不适合我
    • 不,不工作。您可以查看here。我已经在清除缓存的新 Firefox 和 Chrome 隐身窗口中进行了测试,没有一个有效
    • 因为这是一个 Symfony2 项目,所以我们将有 http://domain.com/combivent?email=WGA1cUtKZi4 用于生产环境,http://domain.com/app_dev.php/combivent?email=6ozk_zxZu-A 用于开发环境(share.qa.pdone.com 是真正的域)。 .htaccess 位于项目根路径上的 /web 文件夹中。 app.php是处理一切的脚本,看看编辑过的帖子
    • 不工作但它重定向到http://share.qa.pdone.com/var/www/html/mt020415-00-pdonereptool/web/?email=6ozk_zxZu-A应该是http://share.qa.pdone.com/?email=6ozk_zxZu-A
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多