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'));
Related
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.
If I type this: "http://examplepage.com/gallery/examplecagegory/1-test-picture.jpg" to the browser.
Go to webroot:
"app/webroot/gallery/pictures/1.jpg"
I tried:
Router::connect('/gallery/:slug_category/:id-:slug.:extension',
array('webroot/gallery/pictures'),
array(
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
);
But I stucked in the second row... :-/
This is not something you can do with routing, as the book states:
Routing is a feature that maps URLs to controller actions.
An image resource is not a controller action. You should just use a plain RewriteRule in the .htaccess file in app/webroot to rewrite all calls. Something like this should do the trick:
RewriteRule ^gallery/[a-z]+/([0-9]+)-[a-z-]+\.([a-z]{3})$ /gallery/pictures/$1.$2
Please do be aware that the HtmlHelper searches for images in the app/webroot/images folder by default, so you will need to use absolute URLs (prefix all image calls with a leading slash) to use your rewritten path, for example this will not work:
$this->Html->image('gallery/examplecategory/1-test-picture.jpg');
You should use this instead:
$this->Html->image('/gallery/examplecategory/1-test-picture.jpg');
I have a new question related to this question, but since the questions do not match at all, I am creating this new topic. But it is for good reference.
I changed my mind about the logs to log thing, logs is fine. But I am having another controller called WikisController, in this case I would really like to change the URL. I just hate it that the URL will be example.com/wikis/subject.html, I want it to be example.com/wiki/subject.html. It is much cleaner and the convention is now a 100% match with other sites where it is also called wiki, and not wikis. But, I want the controller to be called WikisController, because of the CakePHP convention.
Well, now comes the real problem. I was currently using the following routing in Config/routes.php:
Router::connect('/wiki/:action/*', array('controller' => 'wikis'));
Router::connect('/wiki/*', array('controller' => 'wikis'));
This is the piece of code used by the solution in the other topic. But now I am running into a little bit of trouble. Well, it isn't really a big issue because everything works just fine. But I don't like the URL.
When I create a link as the following from example.com/wiki/edit.html:
echo $this->Html->link('Wiki overview', array(
'controller' => 'wikis',
'action' => 'index',
'ext' => 'html'
));
It is creating a link to example.com/wiki/index.html totally fine and it works, but I don't want to show the index.html, I just don't. It is a word extra in the url and it is not necessary for the user to understand where he is.
I am creating the link with the controller and action key because of a little bug I was having earlier. When I didn't specify the action, it would create a link to example.com/edit.html which I don't want, obviously. So I have to add the action => index key to the array. I am not sure if this is a real bug, but it shouldn't matter. Index = index and nothing changes that. It is a good thing that I am sure my URL is pointing to the right page, so adding the action isn't an issue for me.
Just for good notice:
When I am creating a link on exactly the same way as I did above to example.com/flights/index.html from the page example.com/flights/add.html it would delete the /index part from the url and simply create a url to example.com/flights.html which is much cleaner.
So it has to do something with the routing, but I can't figure out what.
If I understood you correctly, then you'll need a route that connects /wiki to the controllers index action:
Router::connect('/wiki', array('controller' => 'wikis', 'action' => 'index'));
Router::connect('/wiki/:action/*', array('controller' => 'wikis'));
...
I have a route:
Route::set('foo/subdir', '<directory>/<variable>/subdir/<controller>/<action>');
I would like to route this url to the following controller/action:
/application/classes/<directory>/subdir/<controller>.php::action_<action>()
I already have and need this route too, which complicates things:
Route::set('foo', '<controller>/<variable>/<action>');
Is that possible?
Why not, as long as the default route is defined after the directory route.
Route::set('foo/subdir', '<directory>/<variable>/subdir/<controller>/<action>')
->defaults(array(
'directory' => 'default_directory',
'controller' => 'index',
'variable' => 'default_variable',
'action' => 'index',
));
Kohanas routing supports directories 'natively', there is no need to hack anything.
Please note your class names will have to include the directory name as well.
I would like to append the subdir to the directory
This will be possible in Kohana v3.3 using the new Route::filter functionality. There is currently no way to do this in Kohana 3.1 or 3.2 without modifying the Route and/or Request classes.
Use REGEXP to catch directory and subdirectory as /directory/subdirectory/controller/action
to match Route like // where regexp allows you to put / inside directory. Then make little modification in your Route class to change all / to the _
It is not tested ... yet. ;) But im about to...
Trying to learn Kohana, coming from Asp.Net MVC 3. In MVC I am used to the default route (very similar to the default route in Kohana) working like this:
The default route matches any of these:
/
/Home
/Home/Index
I expected it to be the same in Kohana, but it seems all it matches is this:
/
Here's my setup in bootstrap.php:
Kohana::init(array(
'base_url' => '/kohana',
'index_file' => FALSE
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
So if I enter localhost/kohana in the address bar I get to the view called by controller home and action index (action_index). But if I enter localhost/kohana/home/index I get an error saying the object doesn't exist.
Why is this? Shouldn't I be able to enter controller and action in the url and get the correct routing? So basically I have no idea how to enter URLs to get to an action method...
Sorry if this is a stupid newbie question, but I can't figure it out Googling and looking at the Kohana docs... I've been sort of spoiled by the fact that Asp.Net MVC routing always just worked, so I never had to really learn about it...
Rename example.htaccess to .htaccess, open it and change the line RewriteBase / to RewriteBase /kohana/. Windows explorer will probably not allow for a file without the name, so you have to use another file manager (Total Commander for example).