Reputation: 1852
My SEO wants me to rewrite index.php to index.html.
The probnlem is that I am using 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.
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.
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.
Do I have to rename my index.php file to zend.php, and then, modify the last line to :
RewriteRule ^.*$ zend.php [NC,L]
Or do I have to write a 1500 line long action helper code to make it the Zend way?
Upvotes: 1
Views: 1203
Reputation: 161
Tim is right, this is about routing strategy within your Zend project rather than modifying .htaccess.
Only two lines of code (tested):
In your 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.
Tim is also right about doing this is pointless in SEO. You do need better SEO people.
Upvotes: 1
Reputation: 2363
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.
Giving priority to the index.html rather than index.php makes people visit mywebsite.com and see the AngularJS app. 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. 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. 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. Assuming that your AngularJS app uses the suffix .html for its pages
// 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. With this rule you can visit all your AngularJS endpoints directly and refreshing the page will work.
If you have webservices or other endpoints that needs the ZF app they'll continue to work.
But this is not enough to implement SEO in your website: even if direct link is now working, crawlers do not interpret 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 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.
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).
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):
RewriteRule ^*.html$ /index.html
Upvotes: 3
Reputation: 5772
Pending a better solution, maybe this can help you:
Create an index.html with a redirection like this:
<html>
<head>
<meta http-equiv="refresh" content="0;URL=index.php">
</head>
<body>
</body>
</html>
Upvotes: 1
Reputation: 143876
Don't you just want something like this? (Before the rules you have, just below the RewriteEngine On
):
RewriteCond %{THE_REQUEST} \ /+index\.php
RewriteRule ^ /index.html [L,R=301]
RewriteRule ^index.html$ /index.php [L]
Upvotes: 1