简体   繁体   English

Zend,将index.php重定向到index.html

[英]Zend, redirect index.php to index.html

My SEO wants me to rewrite index.php to index.html. 我的SEO希望我将index.php重写为index.html。

The probnlem is that I am using ZEND. 问题是我正在使用ZEND。 When I go to index.php, I lands on my custom exception page, because Zend maybe don't want to understand that some SEO people that are external to PHP don't want to get index.php redirects to an exception page. 当我转到index.php时,我会进入我的自定义异常页面,因为Zend可能不希望了解PHP外部的某些SEO人员不希望将index.php重定向到异常页面。

I understand Zend wants it all for PHP (And I am waiting them to rewrite an AngularJS version into PHP for Zend3) but.... meanwhile, I am stuck with Zend at the moment, and I can't figure out how I can modify my .htaccess. 我知道Zend希望PHP拥有全部功能(并且我正在等待他们将AngularJS版本重写为PHP for Zend3),但是...。与此同时,我暂时还停留在Zend上,我不知道该如何做。修改我的.htaccess。

At the moment, it looks like this : 目前,它看起来像这样:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

If I add : 如果我添加:

RewriteRule ^index.php$ index.html [NC,L]

This breaks Zend. 这打破了Zend。

Do I have to rename my index.php file to zend.php, and then, modify the last line to : 我是否必须将index.php文件重命名为zend.php,然后将最后一行修改为:

RewriteRule ^.*$ zend.php [NC,L]

Or do I have to write a 1500 line long action helper code to make it the Zend way? 还是我必须编写一个1500行长的动作帮助程序代码才能使其成为Zend方式?

I think I know what you are trying to do, tell me if I understand your background: you have an application that uses AngularJS for the frontend and Zend Framework (and other related technologies) for the backend. 我想我知道您想要做什么,请告诉我我是否了解您的背景知识:您有一个使用AngularJS作为前端的应用程序,并使用Zend Framework(和其他相关技术)作为后端的应用程序。

Giving priority to the index.html rather than index.php makes people visit mywebsite.com and see the AngularJS app. 优先使用index.html而不是index.php可以使人们访问mywebsite.com并查看AngularJS应用。 Then they navigate the website and everything just works fine. 然后他们浏览网站,一切正常。

Here the issue: from the homepage they visit the url /path/to/content.html (or any other endpoint registered in your AngularJS app) and click the refresh button (or try to visit your website using directly the link /path/to/content.html) and what they see is a Zend exception. 问题在于:他们从首页访问url /path/to/content.html(或在AngularJS应用中注册的任何其他终结点),然后单击刷新按钮(或尝试直接使用链接/ path / to访问您的网站/content.html),他们看到的是Zend异常。 Why? 为什么?

Because the /path/to/content.html doesn't exists as a file and there is no route registered in ZF that corresponds to that request. 因为/path/to/content.html作为文件不存在,并且在ZF中没有注册与该请求相对应的路由。 How to fix this? 如何解决这个问题?

I can give you the solution I adopted, but I warn you that I use nginx and not apache, so you have to translate there rules. 我可以给您我采用的解决方案,但是我警告您,我使用的是nginx而不是Apache,因此您必须翻译那里的规则。 Assuming that your AngularJS app uses the suffix .html for its pages 假设您的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;
}

Using this rule I am forcing the webserver to use the index.html when any request that ends with .html is received. 使用此规则,当接收到任何以.html结尾的请求时,我将强制Web服务器使用index.html。 With this rule you can visit all your AngularJS endpoints directly and refreshing the page will work. 使用此规则,您可以直接访问所有AngularJS端点,然后刷新页面即可。

If you have webservices or other endpoints that needs the ZF app they'll continue to work. 如果您有需要ZF应用程序的Web服务或其他终结点,它们将继续工作。

But this is not enough to implement SEO in your website: even if direct link is now working, crawlers do not interpret js. 但这还不足以在您的网站中实现SEO:即使直接链接现在可以使用,抓取工具也无法解释js。 To solve this issue you need to serve crawler with the already interpreted version of your page. 要解决此问题,您需要为搜寻器提供页面的已解释版本。 To make this work you will need a service called prerender.io (and a couple of other rules in the web server config). 为了完成这项工作,您将需要一个名为prerender.io的服务(以及Web服务器配置中的其他两个规则)。 Prerender.io internally uses PhantomJS that is like a command line browser that makes possible run js server side, so you will be able to serve an already composed webpage to crawler. Prerender.io内部使用PhantomJS,就像命令行浏览器一样,它可以在js服务器端运行,因此您将能够为爬虫提供一个已经组成的网页。

If you want to see a project of mine that uses AngularJS and have SEO please visit neobazaar.com , it is far from being a perfect classifieds website but it has directlink and SEO, and uses AngularJS and ZF2 (sourcecode in github). 如果您想查看我的使用AngularJS的项目并进行SEO,请访问neobazaar.com ,它远不是一个完美的分类网站,但它具有Directlink和SEO,并使用AngularJS和ZF2(github中的源代码)。

EDIT #1 This page can help with Apache mode rewrite regexp .You can test the follow to see if it fits (I didn't test it): 编辑#1此页面可以帮助Apache模式重写regexp 。您可以测试以下内容以查看它是否合适(我没有测试过):

RewriteRule ^*.html$ /index.html

Don't you just want something like this? 你不只是想要这样吗? (Before the rules you have, just below the RewriteEngine On ): (在拥有规则之前,在RewriteEngine On下方):

RewriteCond %{THE_REQUEST} \ /+index\.php
RewriteRule ^ /index.html [L,R=301]

RewriteRule ^index.html$ /index.php [L]

Pending a better solution, maybe this can help you: 等待更好的解决方案,也许可以帮助您:
Create an index.html with a redirection like this: 使用以下重定向创建index.html:

<html>
    <head>
        <meta http-equiv="refresh" content="0;URL=index.php">
    </head>
    <body>
    </body>
</html> 

Tim is right, this is about routing strategy within your Zend project rather than modifying .htaccess. Tim是正确的,这与Zend项目中的路由策略有关,而不是修改.htaccess。

Only two lines of code (tested): 仅两行代码(经过测试):

In your bootstrap.php: 在您的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')));
}

Now try visit yourdomain/index.html. 现在尝试访问yourdomain / index.html。

Tim is also right about doing this is pointless in SEO. Tim在SEO中做到这一点也是毫无意义的。 You do need better SEO people. 您确实需要更好的SEO人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM