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.
Related
I am new in cakephp trying to loading default controller as Pages
This is my route :
Router::redirect ('/', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/pages/**', array('controller' => 'pages', 'action' => 'display'));
When I run "http://localhost/project/index.php" then its working fine but try with "http://localhost/project/" its not loading default controller (Pages)
Without htaccess & with htaccess its giving same issue.
This is error:
Controller class ProjectController could not be found.
Error:
The requested address '/project/index.php/project/' was not found on
this server.
You are running in subdirectory, so you should set RewriteBase:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /project/index.php?url=$1 [QSA,L]
</IfModule
As per cakephp manual :
http://book.cakephp.org/3.0/en/development/routing.html#redirect-routing
You should try
Router::scope('/', function ($routes) {
$routes->redirect(
'/home/*',
['controller' => 'Articles', 'action' => 'view'],
['persist' => true]
// Or ['persist'=>['id']] for default routing where the
// view action expects $id as an argument.
);
})
Instead of Router::redirect.You should try this method once, might issue got resolved.
Its resolved
Added baseUrl in app controller:
function beforeRender(){
$this->set('baseUrl', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));
}
Removed App.baseUrl from Core.php :
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
now its working fine on:
http://localhost/app/
There may be a lot of question regarding this but no help from these all . . I have the same issue as On this link.
I am requesting from url http://example.com/projects/phase/test_phase2#5591409f-ce8c-4a8a-a92d-77360a11ef94 to save the progress and this is requesting on the url http://example.com/phases/update_progress . now i want to redirect my page on back to the url . I am using these all . But no luck .
$this->redirect($this->referer('/'));
$this->redirect($this->referer());
$this->redirect($this->request->referer());
$this->redirect( Router::url( $this->referer(), true ) );
after this it is redirecting to http://example.com/phases/example.com But i want it to be redirect on http://example.com/projects/phase/test_phase2#5591409f-ce8c-4a8a-a92d-77360a11ef94.
routes.php is
Router::parseExtensions('json');
Router::connect('/', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/admin', array('controller' => 'dashboard', 'action' => 'index', 'admin' => true));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
.htaccess is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
it is working very well on localhost . All files are same on both server.
Is there any other method to do this ??
I tried to solve this issue by creating a HistoryComponent that saves the requests in the user's session (including the get parameters). The obvious disadvantage of this approach is that it relies on sessions. Since the session is the same in all tabs the back buttons might influence each other in a multi-tab environment.
If you want to use it:
Add the plugin in bootstrap.php
Use the previous_page variable in your view (pass it to the back button)
I have the following url structures in my Yii2 application
http://127.0.0.1/frontend/web/index.php?r=site%2Flogin
http://127.0.0.1/frontend/web/index.php?r=site%2Fpage2
http://127.0.0.1/frontend/web/index.php?r=site%2Fsample
http://127.0.0.1/frontend/web/index.php?r=site%2Fsignup
How can I convert that URL to something like
http://127.0.0.1/login.php
http://127.0.0.1/page2.php
http://127.0.0.1/sample.php
http://127.0.0.1/signup.php
I should remove frontend/web/index.php?r=site%2F
I tried like and it didn't work
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use http://127.0.0.1/frontend/web/
RewriteCond %{HTTP_HOST} !^http://127\.0\.0\.1/frontend/web/$
RewriteRule ^(.*) http://127.0.0.1/frontend/web/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^login\.php$ /index.php?r=site%2Flogin [L]
I also tried like and it didn't work too.
RewriteEngine on
RewriteRule ^frontend/web/index.php?r=site%2F([^\./]+) /$1.php [L]
No need to change .htaccess to achieve this. Adjust urlManager component.
Add this to your application config:
'components' => [
'urlManager' => [
'enablePrettyUrl' => true, // Cancel passing route in get 'r' paramater
'showScriptName' => false, // Remove index.php from url
'suffix' => '.php', // Add suffix to all routes (globally)
],
// Compare requested urls to routes
'rules' => [
'login' => 'site/login',
'page2' => 'site/page2',
'sample' => 'site/sample',
'signup' => 'site/signup',
],
],
As for removing controller part from all other routes - it violates key MVC concepts.
How you define to which controller requested action belongs in that case?
And what in case of actions with the same name?
For example: http://127.0.0.1/create.php - should it load site/create or users/create?
Also I don't sure if it's good practice, but you can write comparisons to all routes the same way with rules, but all action names should be unique.
Conclusion: you can modify urls to desired view as I mentioned above, but omitting controller names is only recommended for default controller (SiteController).
Official documentation:
UrlManager
$enablePrettyUrl
$showScriptName
$suffix
$rules
Routing and URL Creation
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
I have controller action /posts/sitemap where the sitemap is generated. Now i want to point http//yoursite.com/sitemap.xml to /posts/sitemap/
Is there any way I can changes routes.php to fix this.
I appreciate any help.
You can use apaches rewrite engine. Add this to your .htaccess
RewriteEngine on
RewriteRule ^sitemap.xml$ /posts/sitemap/
EDIT
Ok, after your edit I must admit, that my answer doesn't really belong to your question. I don't have much experience with Cake, but maybe adding this to your routes.php will help:
Router::parseExtensions('xml');
Router::connect('/sitemap', array('controller' => 'posts', 'action' => 'sitemap'));