[英]Redirect|Forward from old URL to new URL
I had a route and a method that manipulated URLs in this way: 我有一条以这种方式操纵网址的路线和方法:
http://domain.com/combivent?email=WGA1cUtKZi4
Now that URL has changed into this one: 现在,URL已更改为该URL:
http://domain.com/?email=WGA1cUtKZi4
But old emails still keep the old one. 但是旧电子邮件仍然保留旧电子邮件。 I need to redirect each URL from the old way to the new one. 我需要将每个URL从旧方法重定向到新方法。 For example if I call this http://domain.com/combivent?email=WGA1cUtKZi4
then I should redirect to http://domain.com/?email=WGA1cUtKZi4
how I can achieve this in Symfony2? 例如,如果我将其称为http://domain.com/combivent?email=WGA1cUtKZi4
那么我应该重定向到http://domain.com/?email=WGA1cUtKZi4
如何在Symfony2中实现此目标? Can I use any kind of wildcard in routes or do I need to keep both methods and handle this internally? 我可以在路由中使用任何类型的通配符,还是需要保留这两种方法并在内部进行处理? Can this be done at .htaccess
level? 可以在.htaccess
级别完成此操作吗? Any advice? 有什么建议吗?
This is how the .htaccess
looks like: .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>
Furthermore this domain.com
is a Alias of a VirtualHost that share three domains. 此外, domain.com
是共享三个域的VirtualHost的别名。 Think in the VH definition as: 在VH定义中认为:
ServerName maindomain.com
ServerAlias domain.com
ServerAlias domain1.com
Update 更新资料
Below is the full .htaccess
definition: 以下是完整的.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>
Rather than digging into the .htaccess
you could just redirect the old route to the new route in your routing. 无需深入研究.htaccess
您只需将旧路由重定向到路由中的新路由即可。
new_route:
path: /
defaults: ...
old_route:
path: /combivent
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: new_route
permanent: true
As you are using annotations you would just need to either name your new route or find out the generated name and use that in your "old_route" definition. 使用注解时,只需命名新路线或找出生成的名称,然后在“ old_route”定义中使用该名称即可。
Updated with your actual setup 更新了您的实际设置
PDOneBundle/Resources/config/routing.yml PDOneBundle /资源/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 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)
{
...
}
}
Just add the following rule to your .htaccess
file: 只需将以下规则添加到您的.htaccess
文件中:
RewriteEngine On
RewriteRule ^combivent?email=([^/]+)$ /?email=$1 [L,QSA,R=301]
Edit: You need to add it above the following line: 编辑:您需要将其添加到以下行上方 :
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
This rule is problematic: 这条规则是有问题的:
# Remove combivent word from the URL when call a brand page
RewriteRule ^combivent?email=([^/]+)$ /?email=$1 [L,QSA,R=301]
as you cannot match QUERY_STRING
using RewriteRule
. 因为您无法使用RewriteRule
匹配QUERY_STRING
。 Use a RewriteCond
instead: 改用RewriteCond
:
# Remove combivent word from the URL when call a brand page
RewriteCond %{QUERY_STRING} ^email=[^&]+ [NC]
RewriteRule (?:^|/)combivent/?$ / [L,R=301]
QUERY_STRING
will be carrier over to nee target automatically. QUERY_STRING
将自动带到另一个目标。
Full .htaccess: 完整的.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]
Also make sure to clear your browser cache before testing this. 在测试之前,还请确保清除浏览器缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.