Angular JS Pretty url conflict with cakephp - php

I'm using Angular JS along with cakephp version 3.x. For pretty URL for angular js state routing, i used the following code to achieve.
Added in config,
$locationProvider.html5Mode(true);
Added base in Pages/home.ctp (cakephp file. It's like an index.html)
<base href="/">
It actually works until i reload the page. Also, I've some controller and actions like example.com/contact, example.com/work etc.. which serves a normal php page.
For example :
Before pretty url :
I've a state url example.com/#/profile
Also I've a normal php page example.com/profile
This actually works until i added a html5Mode.
After adding pretty URL :
Angular JS state url becomes : example.com/profile (that's fine)
But if i reload that, it generally throws error as it's actually looking for general MVC page, not angular.
I'm sure that there's is conflict with Angularjs - Cakephp. Plus I've some authentication like only the logged in users can only access certain actions, if not they will be redirected to homepage.
I tried putting server side code in .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It doesn't work.

Related

Laravel shows 'index of' page only on certain routes

I've noticed some people have had similar issues to this, but the issue has been to do with their vhosts and document roots, but most of the URLS on my site seem to be working fine.
For example, if I create the following routes
Route::get('cases', function(){
dd('here');
});
Route::get('bookings', function(){
dd('there');
});
my.local/bookings works absolutely fine, but my.local/cases just shows me the index of /cases page.
I have rolled back my code to a time it was definitely working, but its still returning the index of /cases page.
Why would laravel randomly stop some routes working?
Heres what i've looked at so far
Rolling my code back to a working state
Dumping composer autoload
Checking php artisan route:list to make sure it exists
Trying a server alias to see if a local IP would work instead
Changing my PHP version
It just wont show me this one page?
Any ideas?! Its driving me mad!
Note - /cases/create works fine, my index / page works fine, it just seems to be a GET request to /cases
The reason for this behavior is the default .htaccess rewrite rule:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The two conditions define that the request should only be forwarded to index.php (and therefore Laravel) if there isn't an actual file or folder matching the URL.
It seems like there is a cases folder inside public, so Laravel will not handle requests to that folder.
You can either rename the public/cases folder / the route or remove the first RewriteCond condition. But removing it might have unintended side-effects.

Laravel is "listening" to index.php string anywhere in the URL

I have a Laravel 5 project with routes set up as well as a custom 404 page (mostly for missing/incorrect "pages").
So basically if I open any existing route I get the correct output and every other URL is showing the 404:
project.com/login - Fine, login page
project.com/ghdkfgl - 404
This looks clear and seems to be working as expected. So anything I add after the slash opens either an actual existing page or a 404 page.
Unless I put a 'index.php' anywhere in the URL. In this case, Laravel is executing the request for some reason like this:
project.com/jhdfkds/index.php/login - Opens the login page (the CSS and other resources are gone because of the paths but that's clear).
project.com/kfhjdsg/index.php/fkdhsg - Opens a 404 (but the CSS and other resources are not loaded too).
I'm sure both of these should open the 404 since there's no such routes in my project.
I also checked for the same behavior on the Laravel documentation website (I assume its built on Laravel).
http://laravel.com/docs/5.0 - Actual URL
http://laravel.com/aaa - A nice 404 page
http://laravel.com/aaa/index.php/docs/5.0 - Laravel documentation page again, same as the first one
What might be causing this? How can this be solved?
Why would Laravel even consider the 'index.php' in the middle of the URL?
Does this have anything to do with the .htaccess file? (I didn't edit it though)
The problem is inside the Symfony Request class and how it determines the base URL. Basically it assumes that if you have a request like aaa/index.php that your currently running script (named index.php) is inside the directory aaa takes aaa/index.php as base URL.
Later it will then be stripped from the actual request URI.
I fixed this unwanted behavior with a pull request that is currently under review. I will update this post as soon as it gets merged.
Same issue i was facing in laravel 3 when i was skipping public from url. then i have placed another htaccess file in public folder.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Zend, redirect index.php to index.html

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.

URL variable sending and receiving

How does instagram.com pass the username variable like "instagram.com/username" or like
instagram.com/floydmayweather
without using the $_GET function and it does not turn out looking like this
instagram/index.php?username=floydmayweather
Use a URL rewrite command in your HTTP server. There are many examples out there for both Apache and nginx.
The rewrite rule happens at the server level before it hits your code. This means the URL doesn't actually have to get modified before your code receives it.
The way I do it is I configure Apache/nginx to send all URLs that do not match an existing file (so that static files like images, js and css still work) to my index.php file. Then in the index.php file I parse the URL to determine what page type to load and what data.
In your example, they would grab the last token off the URL, know that it would be a user's name in URL format, look up that user in the database and build the page accordingly.
This is where something like a front controller or URL router comes in to play in most frameworks. In index.php I would map each URL, based on its components, to a class that would then handle the actual page building.
Here is more info on the rewrite modules;
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://wiki.nginx.org/HttpRewriteModule
Some quick Googling will show you many examples for how to configure this.
Your index.php file can examine the $_SERVER array to determine the URL that has been requested. In this situation, the explode() function is your friend, for parsing the URL and checking its components :)
The Rewrite engine will be a perfect solution, for example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Rewrite engine - A rewrite engine is software located in a Web application framework running on a Web server that modifies a web URL's appearance. This modification is called URL rewriting. Rewritten URLs (sometimes known as short, fancy URLs, search engine friendly - SEF URLs, or slugs) are used to provide shorter and more relevant-looking links to web pages. The technique adds a layer of abstraction between the files used to generate a web page and the URL that is presented to the outside world.
Usage
Instead getting URL with extenstion link (.php / html etc..)
www.stackoverflow.com/index.php
You will get URL Without extenstion
www.stackoverflow.com/index

.htaccess + PHP to create dynamic Facebook-type navigation with some fixed links?

I'm trying to create a fluid and dynamic interface, and I do love the way Facebook loads pages so that bits of the page are left intact (most importantly the chat section, top nav bar and footer), while the content you navigate to gets loaded asynchronously.
So.. What needs to be done in order to achieve such a system? What I'm thinking, is to have all link requests rerouted through .htaccess to some main "hub" PHP file that includes whatever content is requested by including the corresponding PHP file that handles that request. So if we were to request /photos in our nav bar, our request would go the .htaccess file, which would reroute the request to some mainhub.php file which in turn would include our photos.php file.
But I'm bumping into a few hiccups. If i reroute all traffic to mainhub.php with let's say this rule: RewriteRule ^(.*)$ mainhub.php [QSA] then everything (images, css, js files etc) will also pass through the mainhub.php file. That's no good..
Also, just as Facebook has some pages that are not loaded asynchronously, but are properly navigated to (like the settings page), I'd also like to have that feature. So also there as to be a requirement for some pages not go through the mainhub.php file.
Another thing I'm not sure how to accomplish, is having the "sticky" content loaded when accessing a specific link. For example /home could be the home page and I could have the sticky bits hardcoded into the home.php file, and when the user navigates the website - request other pages and not include the bits that are static. Seems logical. But what happens if a user jumps into another page (not the /home page where I have the sticky parts coded), what do I do then?..
Maybe I'm thinking the wrong way with this, so I'm hoping some .htaccess + PHP guru could help me out with this.
Rerouting can be done with htaccess. All files, directories and symlinks will still be accessible, everything else is redirected to (in this case) index.php.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
What you probably want is to do some asynchronous loading with ajax. This is completly independent of any .htaccess rerouting.
For the ajax part: You could set a special css class to all links that should use ajax loading. Then use jQuery selector looking for this special class. You can set an on link click event that loads via ajax aswell. You'll probably want to use on of the bigger javascript frameworks like jQuery for this.
By checking the headers - you're able to distinguish between a non ajax call and an ajax call.

Categories