PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label url-routing. Show all posts
Showing posts with label url-routing. Show all posts

Wednesday, June 29, 2022

[FIXED] How do I 410 a pattern of toxic Prestashop URLS?

 June 29, 2022     .htaccess, http-status-code-410, prestashop, url-routing     No comments   

Issue

I've been trying to 410 a pattern of a few thousands toxics URLS that got indexed by Google for one of my clients. I want every urls of the website that contain ?% to send a 410 response code so they got de-indexed.

Example of an URL :

https://www.example.com/fr/promotions?%25252525253Bid_lang=6&p=6

I've tried to put this in the .htaccess, above the If module section, as I found on another thread here, but it didn't work, any ideas ?

RewriteRule ^[a-z]shop($|/) - [G]

Solution

I want every urls of the website that contain ?% to send a 410 response code

Using mod_rewrite at the top of the .htaccess file:

# Send 410 for any URL where the query string starts with "%"
RewriteCond %{QUERY_STRING} ^%
RewriteRule ^ - [G]


Answered By - MrWhite
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, March 16, 2022

[FIXED] Routing for multiple hosts in Cakephp

 March 16, 2022     cakephp, cakephp-4.x, url-routing     No comments   

Issue

As cookbook says:

Routes can use the _host option to only match specific hosts. You can use the *. wildcard to match any subdomain.

But what if I would like set same route for multiple hosts at once?

For example:

$routes->connect(
    '/images',        
    ['controller' => 'Images', 'action' => 'index']
)->setHost('images.example.com');

$routes->connect(
    '/images',        
    ['controller' => 'Images', 'action' => 'index']
)->setHost('images.example2.com');

$routes->connect(
    '/images',        
    ['controller' => 'Images', 'action' => 'index']
)->setHost('images.example3.com');

Above is pointless if I have to set several dozen of those routes.

Ideal would be something like this:

$routes->connect(
    '/images',        
    ['controller' => 'Images', 'action' => 'index']
)->setHosts(['images.example.com','images.example2.com','images.example3.com']);

Solution

That's not supported, you'll either have to set multiple routes accordingly, which you could simply do in a loop that you feed a list of your hosts:

foreach (['images.example.com','images.example2.com','images.example3.com'] as $host) {
    $routes
        ->connect(
            '/images',        
            ['controller' => 'Images', 'action' => 'index']
        )
        ->setHost($host);
}

or create a custom route class that accepts either multiple hosts, or maybe actual regular expressions. The latter would probably be easier, as it wouldn't require reimplementing a lot of stuff for matching, something like:

src/Routing/Route/RegexHostRoute.php

namespace App\Routing\Route;

use Cake\Routing\Route\DashedRoute;

class RegexHostRoute extends DashedRoute
{
    public function match(array $url, array $context = []): ?string
    {
        // avoids trying to match the _host option against itself in parent::match()
        if (!isset($url['_host'])) {
            return null;
        }

        return parent::match($url, $context);
    }

    public function hostMatches(string $host): bool
    {
        return preg_match('^@' . $this->options['_host'] . '@$', $host) === 1;
    }
}

That should allow to set a host like images\.example[2-3]?\.com:

$routes
    ->connect(
        '/images',        
        ['controller' => 'Images', 'action' => 'index'],
        ['routeClass' => \App\Routing\Route\RegexHostRoute::class]
    )
    ->setHost('images\.example[2-3]?\.com');

See also

  • Cookbook > Routing > Custom Route Classes


Answered By - ndm
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, March 8, 2022

[FIXED] Yii UrlManager not working

 March 08, 2022     php, url, url-routing, yii     No comments   

Issue

I am having Yii UrlManager issue.

I want to have following url: localhost/mylist/this-is-demo-content

It is working fine by:

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName' => false,
            'rules'=>array(
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                '<controller:\w+>/<title:\w-]+>' => '<controller>/view',
                '<controller:\w+>/<id1:\w+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            ),

But when I try localhost/mylist/this, it says

no action found "this"

Here mylist is controller and I have one action

public function actionView($title)
{
    echo $title;
}

I have also tried this url pattern:

'<controller:\w+>/<title:[A-Z a-z 0-9 _ -]+>' => '<controller>/view',

but could not work


Solution

Continue from these simplified rules:

    'rules'=>array(
        // Place custom rules here
        'mylist/<title>' => 'mylist/view',

        // Nothing should go below these default rules
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
    ),


Answered By - Samuel Liew
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, February 16, 2022

[FIXED] Cakephp 3.1 - How to redirect based on query string values?

 February 16, 2022     cakephp, cakephp-3.1, redirect, url-routing     No comments   

Issue

We recently updated our site from pure php to cakephp 3 and having trouble redirecting urls that have params to a new url.

For example this redirect works fine

$routes->redirect('/webcast.php', '/webcast', ['status' => 302]);

But if there's parameters, it doesn't work

$routes->redirect('/webcast.php?id=100', '/webcast', ['status' => 302]);

Any ideas?

Thanks.


Solution

The router doesn't support matching on query strings, the URL passed to the router when checking for a matching route won't have any query string values attached anymore.

While it would be possible to workaround this by using a custom routing dispatcher filter, a custom/extended Router class, and a custom/extended RouteCollection class, this seems like way too much work for something that can easily be defined as for example a rewrite rule on server level.

Apache mod_rewrite example:

RewriteCond %{QUERY_STRING} ^id=100$
RewriteRule ^webcast\.php$ /webcast? [L,R=302]

And note that 301 is usually the preferred redirect method.

See also

  • Apache > HTTP Server > Documentation > Version 2.4 > Apache mod_rewrite


Answered By - ndm
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, January 8, 2022

[FIXED] How to overwrite parameters of a Laravel middleware which already is assigned as a group middleware?

 January 08, 2022     laravel, laravel-8, laravel-middleware, php, url-routing     No comments   

Issue

I have a Laravel middleware which accepts parameters. This middleware is assigned to a group of routes on the group level. I need to overwrite the parameters specifically for a single route inside this group. How do I do this?

If I add ->middleware('my_middleware:new_param') to the specific route, then the middleware is executed twice: first with the default parameters from the group level, second with the new parameter.

If I add ->withoutMiddleware('my_middleware')->middleware('my_middleware:new_param') then the middleware is not executed at all.

Example

\App\Http\Kernel:

class Kernel extends HttpKernel {
  protected $middleware = [
    ...
  ];

  protected $middlewareGroups = [
    'my_middleware_group' => [
      'my_middlware:default_param',
      ...,
    ],
  ];

  protected $routeMiddleware = [
    'my_middlware' => \App\Http\Middleware\MyMiddleware::class,
    ...
  ];
}

\App\Providers\RouteServiceProvider:

class RouteServiceProvider extends ServiceProvider {
  public function boot() {
    $this->routes(function () {
      Route::middleware('my_middleware_group')
        ->group(base_path('routes/my_routing_group.php'));
    });
  }
}

routes/my_routing_group.php:

// Neither the following line
Route::get('/my-url', [MyController::class, 'getSomething'])->middleware(['my_middlware:new_param']);
// nor this line works as expected
Route::get('/my-url', [MyController::class, 'getSomething'])->withoutMiddleware('my_middleware')->middleware(['my_middlware:new_param']);

Solution

The answer is simple: One must also repeat the exact parameters in ->withoutMiddleware which one want not to use. This means

routes/my_routing_group.php:

Route::get('/my-url', [MyController::class, 'getSomething'])
  ->withoutMiddleware(['my_middlware:default_param'])   // the original parameters must be repeated, too
  ->middleware(['my_middlware:new_param']);

does the trick.



Answered By - user2690527
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, January 6, 2022

[FIXED] Hiding the controller path in the URL

 January 06, 2022     cakephp, cakephp-4.x, subdomain, url-routing     No comments   

Issue

I have these URLs:

  • https://my.example.com/api/foo
  • https://my.example.com/api/bar
  • https://my.example.com/api/baz

In order to simplify it for the user and also to emphasise the different meaning I have set up these subdomains which have the same DocumentRoot as https://my.example.com/:

  • https://foo.example.com/
  • https://bar.example.com/
  • https://baz.example.com/

Due to the fact I am not capable to do the final step which means the URLs are still like this:

  • https://foo.example.com/api/foo
  • https://bar.example.com/api/bar
  • https://baz.example.com/api/baz

which isn't really simplifying it as the user has still to append api/<name> to the URL.

My question:

Is there a way to omit /api/<name> and redirect it somehow in the CakePHP controller so it's enough to enter https://foo.example.com/ and this would run the code in https://foo.example.com/api/foo?

I can check the current subdomain using $_SERVER ["SERVERNAME"] so this isn't the problem.

What makes it a bit more difficult:

The login page is as usually accessed through https://foo.example.com/users/login which means when /users/login is a part of the URL then the invisible redirect must NOT be triggered. However, I think I can prevent this checking the content of the $_SERVER variable.

I really hope you guys understand what I mean.

PS: I don't want to use Apache's redirect as this would display the final URL https://foo.example.com/api/foo in the browser.

Running CakePHP 4.1.4


Solution

I don't really see how this is more simple, if anything I'd find it to be less verbose, which is never good for a URL. Anyways, it sounds like you're looking for host bound routes, ie the _host option.

This would connect / to foo.example.com, bar.example.com, and baz.example.com, each to a different controller depending on the host:

$routes->scope(
    '/',
    function (\Cake\Routing\RouteBuilder $builder) {
        $builder
            ->connect('/', ['controller' => 'Foo'])
            ->setHost('foo.example.com');

        $builder
            ->connect('/', ['controller' => 'Bar'])
            ->setHost('bar.example.com');

        $builder
            ->connect('/', ['controller' => 'Baz'])
            ->setHost('baz.example.com');

        $builder
            ->connect('/users/login', ['controller' => 'Users', 'action' => 'login']);
    }
);

The /users/login URL would connect to any host, as it's not being restricted, ie all of these URLs would work and point to the same controller and action:

  • foo.example.com/users/login
  • bar.example.com/users/login
  • baz.example.com/users/login

That could possibly introduce further problems, like for example cookies not working if they're not scoped for all subdomains, or duplicate content as far as SEO goes. So make sure that you keep your routes as specific as possible!

Also note that resource routing won't work properly with this, as it requires a path element, ie you cannot connect resources on just /, eg foo.example.com, it would have to be foo.example.com/resource-name, if you'd wanted to connect to the former, each resource route would need to be connected manually.

Finally, if you're not using named routes, then you'll have to use '_host' => 'foo.example.com' in your URL arrays if you want to generate URLs!

See also

  • Cookbook > Routing > Matching Specific Hostnames


Answered By - ndm
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, January 4, 2022

[FIXED] Laravel 5 check whether a user is logged in

 January 04, 2022     authentication, filter, laravel, laravel-5, url-routing     No comments   

Issue

I am new to Laravel 5 and trying to understand its Auth process. I want to prevent user to reach some of my pages unless the user is not logged in. Trying to make it with Route:filter but it does not work. What i have done wrong ?

Route::filter('/pages/mainpage', function()
{
    if(!Auth::check()) 
    {
        return Redirect::action('PagesController@index');
    }
});

Solution

You should use the auth middleware. In your route just add it like this:

Route::get('pages/mainpage', ['middleware' => 'auth', 'uses' => 'FooController@index']);

Or in your controllers constructor:

public function __construct(){
    $this->middleware('auth');
}


Answered By - lukasgeiter
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, December 28, 2021

[FIXED] Basic routing generates a 404 in Laravel

 December 28, 2021     laravel, laravel-routing, mamp, url-routing     No comments   

Issue

I'm trying to use the Laravel framework in building my application. However, I'm getting stuck with routing.

Route

Route::get('ecatalogs', 
    array('as' => 'ecatalog_latest', 'uses' => 'ecatalogs@latest'));

Controller

class Catalogs_Controller extends Base_Controller
{
    public $restful = true;

    public function get_latest()
    {
        return "wohoooooo!";
    }
}

My localhost files are stored in /Users/ariefbayu/Sites/ and my Laravel application is stored in /Users/ariefbayu/Sites/ecatalog/. Inside this directory, I have an info.php file to confirm if my path settings are working, and they do. However, when I navigate to http://localhost/ecatalog/public/index.php/ecatalogs it always returns a 404 error. I know this is basic, but I don't know why this doesn't work.

FYI, I'm using a MAMP server, and I've set all source files' access permissions to 777 to test if this is a permission problem.


Solution

Route::get('ecatalogs', array('as'=>'ecatalog_latest', 'uses'=>'ecatalogs@latest'));

Notice the ecatalogs@latest pointer. This tells Laravel to call the get_latest() method on the Ecatalog_Controller.

And this is your controller Catalogs_Controller and function get_latest(). You need to call the get_latest() with this :

Route::get('ecatalogs', array('as'=>'ecatalog_latest', 'uses'=>'catalogs@latest'));


Answered By - Wahyu Kristianto
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing