我有一个基于 Yii2 高级模板的站点,用于替换无法针对 SEO 优化的旧站点。
在 Yii2 的 urlManager 配置中启用了 SEO 友好的 url。
出于 SEO 目的,我应该从旧站点编写近 20 301 重定向,以获取具有结构的原始查询字符串的 url:
/index.php?p=var
/index.php?p=open_cat&cat_id=55
到 SEO 友好的 url('contoller'、'action'、'alias' 是变量):
/controller/action/alias
我试过:
在前端/web/.htaccess 中:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Redirect 301 /index.php?p=open_cat&cat_id=55 http://example.com/controller/action/alias
RewriteRule . index.php
此方法无效,带有/index.php?p=open_cat&cat_id=55 url 的页面响应 404 错误。
在前端/web/.htaccess 中:
RewriteEngine on
RewriteCond %{QUERY_STRING} p=open_cat&cat_id=55
RewriteRule ^index.php$ /controller/action/alias [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
此方法使用原始路由附加 index.php 的完整路径:
http://example.com/path-to-file-from-server-root/app/frontend/web/index.php?r=controller/action/alias
我不知道如何使用 Yii2 方法为原始 url 编写正确的重定向。 另一个问题是新旧 url 没有任何共同点。 我为所有重定向搜索统一的解决方案,因为新的 url 与不同的操作和 Controller 相关联。
我很乐意提供提示或解决方案。谢谢!
最佳答案
解决方案取自 this question 上的答案.
我已经创建了组件 RedirectComponent,包含在 frontend\config\main.php 的组件和 Bootstrap 数组中:
<?php $params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php') );
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'homeUrl' => '/',
'bootstrap' => ['RedirectComponent','log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
/* */
'RedirectComponent' => [
'class'=>'frontend\components\RedirectComponent',
],
],
'params' => $params,
];
我在 RedirectComponent 类中创建了方法 init() (不确定是否需要在其中的 parent::init() 上):
public function init()
{
/*redirect logic*/
}
我无法通过 redirect() 方法重定向到正确的位置,通过动态创建的 Controller 和操作调用,所以我通过 native php header() 函数重定向。
关于php - Yii2 301 从原始 url 重定向到 .htaccess 中的 SEO 友好 url 不起作用,需要其他解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38413824/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?
我正在编写一个简单的静态Rack应用程序。查看下面的config.ru代码:useRack::Static,:urls=>["/elements","/img","/pages","/users","/css","/js"],:root=>"archive"map'/'dorunProc.new{|env|[200,{'Content-Type'=>'text/html','Cache-Control'=>'public,max-age=6400'},File.open('archive/splash.html',File::RDONLY)]}endmap'/pages/search.