[英]Zend, redirect index.php to index.html
我的SEO希望我将index.php重写为index.html。
问题是我正在使用ZEND。 当我转到index.php时,我会进入我的自定义异常页面,因为Zend可能不希望了解PHP外部的某些SEO人员不希望将index.php重定向到异常页面。
我知道Zend希望PHP拥有全部功能(并且我正在等待他们将AngularJS版本重写为PHP for Zend3),但是...。与此同时,我暂时还停留在Zend上,我不知道该如何做。修改我的.htaccess。
目前,它看起来像这样:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
如果我添加:
RewriteRule ^index.php$ index.html [NC,L]
这打破了Zend。
我是否必须将index.php文件重命名为zend.php,然后将最后一行修改为:
RewriteRule ^.*$ zend.php [NC,L]
还是我必须编写一个1500行长的动作帮助程序代码才能使其成为Zend方式?
我想我知道您想要做什么,请告诉我我是否了解您的背景知识:您有一个使用AngularJS作为前端的应用程序,并使用Zend Framework(和其他相关技术)作为后端的应用程序。
优先使用index.html而不是index.php可以使人们访问mywebsite.com并查看AngularJS应用。 然后他们浏览网站,一切正常。
问题在于:他们从首页访问url /path/to/content.html(或在AngularJS应用中注册的任何其他终结点),然后单击刷新按钮(或尝试直接使用链接/ path / to访问您的网站/content.html),他们看到的是Zend异常。 为什么?
因为/path/to/content.html作为文件不存在,并且在ZF中没有注册与该请求相对应的路由。 如何解决这个问题?
我可以给您我采用的解决方案,但是我警告您,我使用的是nginx而不是Apache,因此您必须翻译那里的规则。 假设您的AngularJS应用程序的页面使用后缀.html
// priority to the index.html, the use index.php
index index.html index.php;
// other content here
// does the url ends with .html? force the request to end to index.html
// you can use any other discriminant
location ~ .*\.(html)?$ {
try_files $uri $uri/ /index.html;
}
使用此规则,当接收到任何以.html结尾的请求时,我将强制Web服务器使用index.html。 使用此规则,您可以直接访问所有AngularJS端点,然后刷新页面即可。
如果您有需要ZF应用程序的Web服务或其他终结点,它们将继续工作。
但这还不足以在您的网站中实现SEO:即使直接链接现在可以使用,抓取工具也无法解释js。 要解决此问题,您需要为搜寻器提供页面的已解释版本。 为了完成这项工作,您将需要一个名为prerender.io的服务(以及Web服务器配置中的其他两个规则)。 Prerender.io内部使用PhantomJS,就像命令行浏览器一样,它可以在js服务器端运行,因此您将能够为爬虫提供一个已经组成的网页。
如果您想查看我的使用AngularJS的项目并进行SEO,请访问neobazaar.com ,它远不是一个完美的分类网站,但它具有Directlink和SEO,并使用AngularJS和ZF2(github中的源代码)。
编辑#1此页面可以帮助Apache模式重写regexp 。您可以测试以下内容以查看它是否合适(我没有测试过):
RewriteRule ^*.html$ /index.html
你不只是想要这样吗? (在拥有规则之前,在RewriteEngine On
下方):
RewriteCond %{THE_REQUEST} \ /+index\.php
RewriteRule ^ /index.html [L,R=301]
RewriteRule ^index.html$ /index.php [L]
等待更好的解决方案,也许可以帮助您:
使用以下重定向创建index.html:
<html>
<head>
<meta http-equiv="refresh" content="0;URL=index.php">
</head>
<body>
</body>
</html>
Tim是正确的,这与Zend项目中的路由策略有关,而不是修改.htaccess。
仅两行代码(经过测试):
在您的bootstrap.php中:
protected function _initHomeRoute(){
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('home', new Zend_Controller_Router_Route('/index.html', array('controller' => 'index', 'action' => 'index')));
}
现在尝试访问yourdomain / index.html。
Tim在SEO中做到这一点也是毫无意义的。 您确实需要更好的SEO人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.