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?
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
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]
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>
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.
Related
I am trying to rewrite URLS so that /foo -> index.php.
index.php is powered by codeigniter 3.
bar.php is a stand alone php file.
This is a snipped from my .htaccess:
//Test 1. Bar.php displayed. This works as exptected.
RewriteRule ^/?foo1/?$ /bar.php [QSA,L]
//Test 2. Redirects succesfully to index.php. Works as expected.
RewriteRule ^/?foo2/?$ /index.php [R=301,QSA,L]
//Test 3. Goes to codeigniter 404 page and does NOT display the homepage.
Does not work as expected
RewriteRule ^/?foo3/?$ /index.php [QSA,L]
Why does Test 3 not display as expected? There is something in my codeigniter code that doesn't work when trying to rewrite URLS. Any ideas how to fix this? Is there a workaround?
Full disclosure: the long term aim is to be able to have a URL with structure like this:
example.com/foo1/foo2/foo3/?query1=xxxx&query2=yyyy
rewrite to
example.com/bar/bar1.php?queryA=foo2&queryB=foo3&query1=xxxx&query2=yyyy
Can this be "easily" done within the codeigniter framework (route.php) rather than htaccess? From a development time perspective, just getting it to work via .htaccess would be the preference.
Take a look at https://gist.github.com/keithmorris/3023560
This includes a fairly standard htaccess file that I use on all my projects. You then use the CI routing functionality to define where different bits go.
Bear in mind with CI you can pass additional elements in a url such as example.com/foo1/foo2/foo3/query1/query2.
I am trying to setup simple url routing in a Perl web project without haveing to include a framework just for that purpose. I believe this can be accomplished with an .htaccess.
The plan is for any request to the server using example.com/anysubdirectory/... to be routed to a perl/php script that will parse whatever is contained in /anysubdirectory/... and the parameters following it and then determine where to send the user based on that info.
If example.com without any subdirectory is requested I need to still maintain the default behavior of searching for an index page here.
Since the /anysubdirectory/ will be dynamic i'm not able to predefine that /123/ -> option 1 or /abc/ -> option 2
I am not overly familiar with htaccess other than the typical www and base rewrites.
Any help is much appreciated.
I believe I answered my own question using the following in the root .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.pl?action=$1 [L,NC,QSA]
This seems to be working with my initial testing.
I believe this is how it is working:
If the requested subdirectory is not found as a file
If the requested subdirectory is not found as a directory
Redirect this to the router.pl script along with any leftover parameters from the original url.
EDIT: The above is not working completely, this is still redirecting any file that is not found on the server to the router.pl script. Not really the functionality that I am looking for,i would like this to only happen if it is a subdirectory and not an invalid file
Not sure I want any bot thats guessing filenames to be pegging my script on a regular basis.
Please correct this response if any of the above is not accurate.
We are building a website where user has to login in order to view site's content (similar to what facebook and twitter use)
The problem is that our site's navigation is completely messed up:
When user opens the site, he is at: sitename.com
When user logs-in, location changes to: sitename.com/login_success.php
when user uses navigation bar, location changes to: sitename.com/login_success.php#page2 (AJAX is used to change the div content)
In comparison to facebook (url):
user is loged-in: sitename.com
user is NOT loged-in: sitename.com
user navigates to friend search: sitename.com/search
user navigates to settings: sitename.com/settings
Why do sites like facebook have so clean URLs? How do they do it? I'd like to create a clean website, with clean/user-riendly URLs (without # or ? and & and =) - where do I start? Do we need to use any framework (yii, zend, etc..)?
yeah, you gotta use mod-re-write.
for example, this is how to change sitename.com/login_success.php#page2 into sitename.come/page2:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
#first, what is the original request
RewriteCond %{THE_REQUEST} /login_success.php#page([0-9]*)
# now use regex to redirect to the clean url structure
RewriteRule ^$ /page%1? [R=301,L,NE]
# now make the clean url serve the content from the ugly one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page([0-9]+) /login_success.php#page$1 [L]
I'm not quite sure about that last regex match, but I hope this gets you on the right track!
I believe considerable amount of coding is done in your case.So it would not be advisable to switch to some framework like yii or zend now, this decision should be taken earlier.
Check how to simplify the url.
You can use mod_rewrite of apache web server.
They use mod_rewrite and similar tools to clean up their URLs.
mod_rewrite is available on Apache. The IIS equivalent is named URL Rewrite.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://www.iis.net/downloads/microsoft/url-rewrite
You don't need to use a special framework to get this to work, but it helps ease the process, as many frameworks have this feature built-in.
One that comes to mind is Wordpress. Wordpress gives you great control over how this works without having to touch the configuration files too much.
I currently run a site with 750 pages of .html webpages (yeah I know it was a stupid idea, but I'm a novice). I'm looking to move these to php. I don't really want to set up 750 individual 301 redirects and rewrite each page to .php
I've heard that I can use htaccess to this. Anyone know how?
A few additional questions -
Can I permanently redirect these links from html to php without losing my search engine rankings and
if I want to add php to each of the files (i.e. a php file menu (using the include command) to make the links quicker to update will this work? Because won't they still be html files?
Sorry for the stupid questions, but I'm still learning.
Congratulations on a 750 page site - you must have put some work into that.
To collect your current list of pages use a tool called xenu to create an export into excel. You can then easily change the name the files to PHP in column b and create a .htaccees file.
However why would you want 750 php files? If you have lots of data pages, make it one page and suck in the HTML main content and reference one page. If you have a page called warehouse-depot-22-row-44.html then change that to show-warehouse-row.php?depot=22&row=44 and return that content only. This will significantly reduce your number of pages and to start using databases to render the content.
For redirecting you could use the Apache Module mod_rewrite: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
You can use url rewriting to match a specific file name request with a regular expression and then decide where to redirect if matched
RewriteRule ^myname/?$ myname.php [NC,L]
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Depends on the structure you have.
You want the user to access them in their natural location?
/public_html/folder1/file.php
user would access like
mydomain.com/folder1/file
or you want to map them differently?
Personally I think I would use a rewrite rule to map all requests to my /public_html/index.php and would map the requests from there using php (using include for instance). This gives great flexibility, plus you have a single point of entry for your application which is very beneficial since you can easily maintain control of the application flow.
The .htaccess would look like this
#
# Redirect all to index.php
#
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !^/index\.php
# RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?))$ [NC]
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
of course I place all my not directly accessible files (everything except index and css, js, images, etc) to a folder outside the public_html to ensure no user can ever access them directly ;)
I've had a similar (yet much much smaller) site that went through the same thing.
I have this in my .htaccess:
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
This will help redirect any visitors to your .html addresses to your .php addresses.
You hopefully have an IDE (I recommend Aptana), and you can use some of the find/change functions project-wide, and hopefully with some time and patience get your internal links from .html to .php.
But, I caution you a little bit - Perhaps it is time to look into a database based CMS, such as Wordpress or Drupal?
I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.