cakePHP URL rewrite rule for static pages - php

I'm using cakePHP. How do I write a rewrite rule for the following condition.
http://www.example.com/pages/about_us
to
http://www.example.com/about
and similar to contact_us and other pages.

app/Config/routes.php
Router::connect('/about', array('controller' => 'pages', 'action' => 'display', 'about_us'));

You could use a 301 redirect like so:
Redirect 301 /about_us http://www.example.com/about

Related

301 Redirects in CakePHP 3

I'm new to CakePHP. I have a website that was built by someone else in CakePHP 3.3.16. There are links to website.com/page and also website.com/page/.
What's the best way to get /page/ redirecting to /page without affecting anything else that might start with a /page/?
The route.php has this...
$routes->connect('/page/*', ['controller' => 'Page', 'action' => 'something']);
Then I have a PageController.php which has
public function something($site_id = null)
{
...
}
Will this work in the routes.php? How would I specify that this is a 301 redirect?
use Cake\Routing\RouteBuilder;
Router::scope('/', function (RouteBuilder $routes) {
$routes->redirect('/page/','http://website.com/page');
$routes->connect('/page/?*', ['controller' => 'Page', 'action' => 'something']);
});
This doesn't seem to work in the .htaccess (/page/ is displayed and not redirected)...
Redirect 301 /page/ http://website.com/page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
as a first glimpse to fix this would be instead of putting the Route::scope would be Router::connect and Router::redirect imo.
Therefore, an approach to a solution would be first doing something like this.
Router::connect('/page/*', ['controller' => 'Page', 'action' => 'something']);
And then you redirect the page with the cake command redirect:
Router::redirect('/page/','/page', array('status' => 301));
In the project that I use which is CakePhp 2.6, I always have redirected pages like this depending on the task. Sometimes you can do this type of redirects inside the Controller but it is best to avoid it for not mixing routing with programming logic.

CakePHP default Controller without changing URL

May be this question is already asked. If it is, please provide the link. I searched but not found what I actually wanted.
I know how to define default controller via Router::connect() but that redirects users to a different place. For example, if I do this for domain www.example.com :
Router::connect('/', array('controller' => 'users', 'action' => 'index'));
People coming to www.example.com will be redirected to www.example.com/users/ (see, both URLs aren't same). My question is, what should I do if I want to connect a controller without adding /:controller so that users coming to www.example.com will be in www.example.com but get contents of UsersController?
Try this on your routes.php after
Router::connect('/login', array('controller' => 'users', 'action' => 'login'))
Now your example.com/users/login should looks like example.com/login and address will work.

CakePHP Router::url not working correctly

I'am using CakePHP 2.0 for a small admin section of a website.
If the user is logged in correctly, I redirect them to the admins dashboard.
I do this as following:
$this->redirect(Router::url(array('controller' => 'admin', 'action' => 'dashboard')));
The redirect is done correct, but for some reason the URL where it redirects to isn't correctly build. The URL is in the following structure (note the double [root] sections in the URL - this is what is wrong):
http://localhost/[root]/[root]/admin/dashboard
Off course their are errors shown because this controller / action doesn't exists. The URL should be off this form:
http://localhost/[root]/admin/dashboard
I can't seem to find what the exact problem is since cakePHP is not my core dessert, is there anyone that can point me in the right direction?
Thanks!
$this->redirect("YOUR URL");
example $this->redirect('/admins/dashboard');
This way you can redirect easily!
You can use the 'full_base' parameter in Router::url.
e.g.
$url = Router::url( array(
'controller' => 'posts',
'action' => 'view',
'id' => $post[id],
'full' => true
) );
This will give you a full base url. I found this fixed routing problems when I was running CakePHP in a subdirectory of localhost.
you can simply right like this
$this->redirect(array('controller' => 'admins', 'action' => 'dashboard'));
You can simply right this
Router::connect('/', array('controller' => 'users', 'action' => 'index'));

Routing search engine friendly URLs for PagesController in CakePHP

I'm try to create search engine friendly URLs for the pages controller, i.e. /about instead of /pages/about.
I've tried setting up the following routes (at the bottom of routes.php):
Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));
and
Router::connect('/:page', array('controller' => 'pages',
'action' => 'display'), array('pass' => array('page'), 'page' => '[a-z]+'));
Both properly match /about, /support, etc. However, failed when I had a action/method pair. For example, /contact should route to PagesController->contact(). However, the above routed it to PagesController->display().
There has to be a way to accomplish this without making a specific route for each page. How can I create a route or set of routes that:
Mimics the default route behavior for
the PagesController. That is routes
to display() unless a action/method
pair exists.
Does so with search engine friendly URL. That is coming from root / not /pages.
Demonstrate both the Router::connect() and Html->link()
I have checked for examples in the CakePHP Book and viewed other questions such as CakePHP routing in pages controller. Nothing seems to satisfy the specification above.
You need to create a second route for the contact method call and put it before the more generic rule to match "everything [a-z] after /pages". Try that before your rule:
Router::connect('/contact', array('controller' => 'pages', 'action' => 'contact'));
Always keep in mind that the order of routes is important. The more generic a rule is the more will it match. So put more specific rules in front of the more generic ones.

CakePHP, How do I accept a parameter for a controller and not use an action?

Would like http://websites.com/users/username1 URL structure
In your app/config/routes.php file you could add:
Router::connect('/users/*', array('controller' => 'users', 'action' => 'index'));
This would route all requests to /users/ to the index action of the users controller. If you want to use a different action, just replace 'index' with the action you prefer to use.
More info is here:
http://book.cakephp.org/view/46/Routes-Configuration

Categories