How to get rid of trash in Kohana Pagination urls? - pagination

i've a question on ko3 framework Pagination module.
I have a route template like this: http://my-site.com/blog/1/page2
Here's the code from my bootstrap.php file:
Route::set('blog', 'blog(/<id>(/page<page>))')->defaults(array('controller' => 'blog', 'id' => 1, 'page' => 1));
everything works nice, but Pagination library generates dirty urls like
http://my-site.com/blog/1/page3?kohana_uri=blog%2F1.
Here's the code that creates the pagination (in Controller_Blog)
$pag = Pagination::factory(array('total_items' => $total_posts, 'items_per_page' => 10, 'current_page' => array('source' => 'route', 'key' => 'page')));
$posts = $posts_model->selectPosts($section_id, $pag->offset, $pag->items_per_page);
$this->template->content = View::factory('html/blog', array('pag' => $pag));
How can I tell the Pagination module generate clean urls? When I remove trash from url manually, it works too.
Thanks in advance

Your .htaccess file has something like this in it: RewriteRule .* index.php?kohana_uri=$0 [PT] which is fine, but setting the kohana_uri GET parameter does absolutely nothing in Kohana 3.x. The rewrite should point to index.php/$0 or just index.php.

Related

Redirect old URL's to new URL pattern

Hi I want to change my current product URL structure to a new one but I want to keep my old ones working because of people who have linked to my products.
How can I do this without manually creating a 301 redirect for each product?
Not sure what Prestashop version you have, just tried in a 1.6.1.6
You can you this override:
<?php
class Dispatcher extends DispatcherCore
{
protected function loadRoutes($id_shop = null)
{
parent::loadRoutes($id_shop);
$language_ids = Language::getIDs();
if (isset($context->language) && !in_array($context->language->id, $language_ids)) {
$language_ids[] = (int)$context->language->id;
}
foreach ($language_ids as $id_lang) {
foreach ($this->default_routes as $id => $route) {
if($id == "product_rule")
$this->addRoute(
'product_rule2',
'{id}-{rewrite}{-:ean13}.xhtml',
'product',
$id_lang,
$route['keywords'],
isset($route['params']) ? $route['params'] : array(),
$id_shop
);
}
}
}
}
In this case I used a second rule for xhtml (without this override it gave a 404). You can change {id}-{rewrite}{-:ean13}.xhtml to the rule you have, then change the rules in SEO to want you want the new one to be.
Please write your ps version. If you use 1.5 or above?
Do you use this structure now {category:/}{id}-{rewrite}{-:ean13}.html ? or
{category:/}{rewrite}{-:ean13}.html or this {category:/}{rewrite}.html
please write more details
depends on it just make htaccess rule
([0-9]+) for id
(.*) for rewrite
and please use RedirectMatch permanent
and set to the result by $1, $2, etc
that guide must help to understand the htaccess url rewriting https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

yii2 pretty url not working on form submission

Here are the rules I am using for the Url Manager.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
'post/<arg1>/<arg2>/<arg3>/<arg4>' => 'post/filter',
'posts' => 'post/index',
],
],
And my .htaccess
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
The rule seems to be working and urls like
post/filter?arg1=9&arg2=0&arg3=d&arg4=3 is getting turned into,
post/9/0/d/3
However, I have a search form like below
$form = ActiveForm::begin([
'action' => Url::to(['post/filter']),
'method' => 'get'
the fields in the form are named arg1,arg2,arg3,arg4. Now whenevr I submit the form the url gets back to the format
post/filter?arg1=9&arg2=0&arg3=d&arg4=3
I am not sure if its got something to do with the rules or the way I am submitting the form (I need submit the form by GET method only). Any help? Thanks.
Try this rule
'post/filter?<arg1:\w+>=<val1:\d+>&<arg2:\w+>=<val2:\d+><arg3:\w+>=<val3:\d+><arg4:\w+>=<val4:\d+>' => 'post/filter/<val1>/<val2>/<val3>/<val4>'

yii2 urlManager + .htaccess = subdomain how to?

here's the challenge: i need to make the imitation of subdomains. Actually all the "subdomains" in the project are simple actions of the main SiteController like
example.com/index.php/site/subsite?id=subname
example.com/index.php/site/about?id=subname
example.com/index.php/site/contacts?id=subname
etc. I need them to look like
subname.example.com/
subname.example.com/about
subname.example.com/contacts
etc. I have included urlManager rules like this:
'rules' => [
'http://<id:\w+>.site.com/about' => 'site/about',
'http://<id:\w+>.site.com/contacts' => 'site/contacts',
'http://<id:\w+>.site.com' => 'site/subsite',
],
And made a .htaccess file that is :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]*).site.com [NC]
RewriteRule %{HTTP_HOST} "^([a-zA-Z0-9]*)$.site.com" "site.com/index.php/subsite?id=$1" [L]
But it (predictably) doesnt work as i am not strong in rewrite rules. What am i doing wrong ?
I would do it like this:
Configure one virtual host on web server to host all subdomain and point to the Yii2 application
Write component Subdomain.php and put it in frontend\components
<?php
namespace frontend\components;
use Yii;
use yii\base\Component;
class Subdomain extends Component {
private $_subdomain = false;
public function init() {
parent::init();
list($this->_subdomain) = explode('.', $_SERVER['HTTP_HOST']);
}
public function __toString() {
return $this->_subdomain;
}
}
?>
In frontend\config\main.php in the 'components' section add:
'subdomain' => [
'class' => 'frontend\components\Subdomain'
],
Then in the project where I need subdomain I would use:
$subdomain = \Yii::$app->subdomain;

CakePHP 301 redirect old URL to friendly URL

I have 2 addresses of the same page (old link and friendly URL link) and both work great! However I need to redirect old link by 301: /instrumenty/show_id/:id to /instrumenty/show/:id-:slug because of SEO (I get :slug argument from 2 fields from database).
I can do this by one line for every page in .htaccess... but it isn't rather good idea ( I have 1000+ these pages).
I tried something like this (in routes.php), but I have no idea how can I get my slug (2 fields from database)
Router::redirect(
'/instrumenty/show/:id-:slug',
array('controller' => 'instruments', 'action' => 'show_id' /* :id and :slug here? how? */),
array('pass' => array('id','slug'), 'status' => '301')
);
You do not need to access database and insert :slug in every redirect link. If you need this just for SEO then redirect '/instrumenty/show_id/:id' to '/instruments/show/:id' and insert canonical tag into each HTML page:
echo "<link rel='canonical' href='/instruments/show/$id-$slug' />";
In routes you will have something like this:
Router::redirect(
'/instrumenty/show_id/*',
array('controller' => 'instruments', 'action' => 'shows'),
array('persist' => true)
);

mod_rewrite URL to a deep link

I was wondering if this was possible in mod_rewrite, and if anyone knows the syntax for it.
Browser receives:
http://example.com/video/my-first-vid/
mod_rewrite redirects to:
http://example.com/#/video/my-first-vid/
Thanks!
It seems you can't use mod_rewrite for redirecting to /#/video/my-first-vid/ as the # sign is urlencoded and it will redirect to http://example.com/%23/video/my-first-vid/ which obviously isn't what you're looking for. I tested this couple of days before on Apache 1.3.
You may create a separate script(s) for redirection. Add a new rules for this:
RewriteRule ^video/?$ /redirector.php?page=video [NC,L,QSA]
RewriteRule ^video/(.*)$ /redirector.php?page=video&subpage=$1 [NC,L,QSA]
But you might also need to parse the query string manually as it could be smth like:
/video/my-first-vid/?refID=test
Here's some simple example on PHP:
<?php
$page = (isset($_REQUEST["page"])) ? trim($_REQUEST["page"]) : "";
$subPage = (isset($_REQUEST["subpage"])) ? trim($_REQUEST["subpage"]) : "";
// setting the redirect URL, your conditions here
if ($page == "video")
{
$url = "/#/video/" . $subPage;
}
else
{
$url = "/"; // some default url
}
header("Location: " . $url);
exit;
?>
I'm not sure about embedding the '#' in the URI, since that's typically a fragment identifier, but you could try this:
RewriteRule "^(?<!#/)(.*)" "#/$1" [R=301,L]
That should redirect anything that doesn't have a '/#' in front of it to the same location with '/#' in front of it.

Resources