How do I get Kohana to call a specific controller? - kohana

My routing is set up like this:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'static',
'action' => 'index',
));
so that typing in:
http://localhost/et/testkohana4/
calls action_index on Controller_Static as it should.
However, when I type in:
http://localhost/et/testkohana4/test
I expect it to say "cannot find Controller_Test" but instead, Kohana misses it and I get a message from Apache that says "The requested URL /testkohana4/index.php/test was not found on this server."
Even when I put in a file under the controller directory called test.php with the class Controller_Test in it, I still get the page-not-found error.
How can I get Kohana to call a specific controller when I type its name in the URL?

Edit: The correct solution as provided in this answers comments was to change the .htaccess RewriteBase value to
RewriteBase /et/testkohana4/
(<controller>(<action>(/<id>)))
There's a mistake in your route. There is no forward slash at the beginning of (<action> ... That should be (/<action> ...
Those <blocks> are dynamic segments in the URL. So in this example:
http://localhost/et/testkohana4/test
Would result in this being called:
Controller: et
Action: testkohana4
ID: test
That should work for you. Hope that helped.

Related

nanoc does not find my PHP files on autocompile

I'm using nanoc to build a static website which has a PHP script for sending mails. Simplified structure:
/content/index.html
/content/contact.html
/content/mail.php
However, when I do a nanoc compile everything is fine in my output folder:
index.html
contact/index.html
mail/index.php
But I can't call the PHP script when doing a nanoc autocompile. /contact/ works, but /mail/ does not.
This is a part of my Rules-file:
route '*' do
if item.binary?
# Write item with identifier /foo/ to /foo.ext
original_filename(item)
else
# Write item with identifier /foo/ to /foo/index.extension
item.identifier + "index.#{item[:extension]}"
end
end
PHP is treated as non-binary. Does anyone know how I can get this to work with autocompile?
If anybody is having similar problems: I found the answer:
The autocompiler does not have support for PHP files. If a directory
is requested, the autocompiler looks for an index.html file in it, but
it will ignore an index.php file. The autocompiler can’t find a MIME
type for it, so it sends the file as application/octet-stream back to
the browser.
It's pretty obvious, but I did not have in mind that autocompile runs a light-weight server with no PHP support (of course).

Add rewrite rule in plugin: with .htaccess in plugin folder or using WordPress functions

I need add a rewrite rule in my plugin, and distribute it with my code. All works fine if I put the rule in the .htaccess in the WordPress root folder, but I need distribute the plugin with my rule.
I try to put a .htaccess inside the plugin folder and try to use the add_rewrite_rule function but doesn't works either.
Here the .htaccess code that works correctly in WordPress root folder but doesn't works in my plugin folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule my-plugin/pages/tp(.*)\.php$ wp-content/plugins/my-plugin/pages/request.php?pid=$1
</IfModule>
I try the follow code in my plugin but doesn't works either:
add_filter( 'query_vars', 'add_query_vars' );
function add_query_vars( $query_vars )
{
$query_vars[] = 'pid';
return $query_vars;
}
add_action( 'init', 'add_init' );
function add_init()
{
$plugin_url = 'the-path-to-my-plugin-folder';
add_rewrite_rule('my-plugin/pages/tp(.*)\.php'
, $plugin_url . 'pages/request.php?pid=$matches[1]','top');
global $wp_rewrite;
$wp_rewrite->flush_rewrite_rules(); // I know this should be called only one time, but I put it here just to keep simple the sample code
}
But I always get the error that the URL wasn't found.
What I'm doing wrong? How can I do what I need? I searched for similar questions but none solve my problem.
My plugin folder structure is:
Main folder: /wp-content/plugins/my-plugin
------ /pages (sub folder)
-------------/request.php (script that should receive the request)
NOTE: WordPress Rewrite API is not the same as Apache Rewrite module.
WP Rewrite API doesn't redirect a request to another URL, it used to
parse current URL and fill query_vars array.
The issue is in the second parameter of you add_rewrite_rule function call. It has to start from index.php? and then there should be your arguments, like pid, for example:
"index.php?pid=$matches[1]...."
So your add_init function should be like this:
add_action( 'init', 'wpse8170_add_init' );
function wpse8170_add_init()
{
add_rewrite_rule('my-plugin/pages/tp(.*)\.php', 'index.php?pid=$matches[1]', 'top');
}
Don't forget to flush rewrite rules by visiting Settings » Permalinks page.
Further reading:
The Rewrite API: The Basics
The Rewrite API: Post Types & Taxonomies
WP handles the plugins from the /wp-admin directory with a PHP script (admin.php), like this:
http://MyWP.com/wp-admin/admin.php?page=MyPlugin/module.php
Therefore, .htaccess files in the plugin directory are not parsed when the plugin is called. They have to be placed in the wp-admin directory or in the root directory, as you already found out.
Although copying the .htacces file to the root directory when the plugin is installed -and deleting it when it is removed- is possible, I don't think it is the best option. Having .htaccess files in the WP space doesn't seem like a good idea.
Your second approach looks much better: Creating rewrite rules in the main script.
Looking at your code, I think the problem are the pattern (Incoming URL string to match) and possibly the substitution URL path ($plugin_url in your question).
The $rule parameter in the add_rewrite_rule() function should capture a segment of the URL (Above) used to call the plugin's modules.
I can't suggest anything else because you don't supply enough information about the plugin and it's directory tree, except what can be guessed from the regex in the rewrite rule. But, this is a general idea of a way to achieve what you need.

URL with encoded slashes goes to 404

I use user-provided content to generate URLs.
One of my URL had for title Kikar Habusiness - émission du 2/12/12, which converted to the URL /url/Kikar+Habusiness+-+émission+du+2%2F12%2F12.
It goes to a 404. If I remove the %2F from the URL it works fine.
An interesting thing is that my php code (using Yii) usually handles 404 with custom pages, but this one returns a default Apache 404. Which leads me to believe it doesn't even reach my bootstrap file.
The .htaccess reads:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
My Yii parseUrl reads:
public function parseUrl($manager, $request, $pathInfo, $rawPathInfo) {
if(preg_match('%^url/(\d+)%',$pathInfo,$matches)){
$_GET['id'] = $matches[1];
return 'url/view';
}
else if(preg_match('%^category/(\d+)%',$pathInfo,$matches)){
$_GET['id'] = $matches[1];
return 'category/view';
}
return false;
}
My URL slug generator is:
public static function slug($title){
$title = ToolBox::trim($title,60,false);
$title = urlencode($title);
return $title;
}
Note that I cannot have basic ASCII URLs, because some of the content is non-latin (such as Hebrew or Arabic).
This is a common problem in Apache and nothing to do with Yii fortunately.
Apache will automatically block any URL it sees with encoded URL parts %2F (/) and %5C (\). It won't even reach any mod_proxy or mod_rewrite rules.
There are a number of ways around this without changing too much code, depending on your environment, including:
"AllowEncodedSlashes" Directive
Double urlencode() values
See here for a full list and instructions: http://www.jampmark.com/web-scripting/5-solutions-to-url-encoded-slashes-problem-in-apache.html
More info as the link is down
If you have access to it, you can enable the AllowEncodedSlashes directive, it's a very old semi-security fix that it's turned off by default anyway. This will get round the problem. If you can't access the Apache configs, then you'll have to look into the other solutions.
The jampark url redirectsto an incorrect page, use the bellow url:
http://www.leakon.com/archives/865

Need help fix 400 Bad Request for CodeIgniter project

In my codeigniter project, i have the following htaccess file to remove the index.php from URL
RewriteEngine on
RewriteCond $1 !^(index\.php|js|media|style|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
I am using the search form (using form helper). Since it is POST based, I have created a controller called "pre_search" to redirect it to search controller so that the POSTed data will be visible in URL and I can use it with URI helper's segment method.
So my pre_search controller
<?php
class Presearch extends CI_Controller {
//just to make form POST data visible in URL string for search
public function index() {
redirect('search/' . $this->input->post('term'));
}
}
And search controller does the real search. For the mean time I have allowed all the characters in URL for testing.
$config['permitted_uri_chars'] = '';
My problem is now when I have percentage sign (%) in URL it shows bad request. I think it is the apache's response and codeigniter have nothing to do with it.
After some research I found that somebody suggested to fix that problem by modifying htaccess like this
RewriteRule ^(.*)$ /index.php/$1 [B,L]
I have tried this method and it breaks everything.
Since % sign was not encoded, I tried to use also in my pre_search controller like this
redirect('search/' . urlencode($this->input->post('term')));
But didn't solve the problem.
So what is the best way to solve this? I know this is apache problem. I am just illustrating my codeigniter codes to clarify my intentions.
Thanks in advance
Deepak
I do the same, these are my params and I also found this link it might help you with the htaccess http://www.dracos.co.uk/code/apache-rewrite-problem/
$this->load->library('form_validation');
$this->form_validation->set_rules('s', '','trim|min_length[3]|required|xss_clean');
if ($this->form_validation->run() == FALSE):
$this->session->set_flashdata('errorMsg', form_error('s'));
redirect($this->session->userdata['redirectUrl']);
else:
redirect(base_url() . $this->lang->line('link_search') . '/' . $this->functions->convertToUrl($this->functions->secureString($this->input->post('s', TRUE))));
endif;
My config values
$config['permitted_uri_chars'] = 'a-z 0-9~%\.\:_\-ñ';
$config['uri_protocol'] = 'PATH_INFO';
I convert the POST to legible url, changing characters to the ones permitted, and then I do the redirect.

Adding subdomain support into lighttpd

I have a lighttpd server with website placed in /home/httpd/example.com/httpdocs and there I have a file called file.php. When I type http://en.example.com/file.php I would like to display file.php that is in default website directory (mentioned above).
So I used document-root described here:
http://redmine.lighttpd.net/wiki/1/Server.document-rootDetails
in this manner:
$HTTP["host"] =~ "en.example.com/file.php" {
server.document-root = "/home/httpd/example.com/httpdocs/"
}
But unfortunately when I type http://en.example.com/file.php into browser I get error 404. What I do wrong and how can I fix it to work?
In the case of the example URL http://en.example.com/file.php, the host is just en.example.com and not en.example.com/file.php (/file.php is the URL path). So try this:
$HTTP["host"] =~ "en.example.com" {
server.document-root = "/home/httpd/example.com/httpdocs/"
}

Resources