Check if URL in the correct format

Asked

Viewed 394 times

0

I need to check if the url is in the right format, and if it is not, redirect to the right url.

In another Controller already has a code that does what I need, I just need to adapt it to another Controller, because the url format is different.

The code is this:

// redirect to real list_route
if($this->uri->uri_string != $this->blog->url_format($post)) {
  redirect(base_url().$this->blog->url_format($post),'location','301');
  exit;

The function with which he makes the comparison in the Model is as follows:

public function url_format($post,$extra=FALSE) {
    $title = isset($post->post_title) ? $post->post_title : $post->title;
    $title = isset($post->url) ? $post->url : $title;
    return $lang_domin.strtolower(url_title($title)).'-postid-'.$post->id.$extra;
}

This code is working perfectly. But the point is that I need to compare to this function:

public function url_format_category($category,$extra=FALSE) {
    $title = json_decode($category->metadata)->{lang('blog_language')} != '' ? json_decode($category->metadata)->{lang('blog_language')} : $category->title;

    $preffix = trim($title) != '' ? '/'.strtolower(url_title($title)) : '';
    $preffix = trim($category->url) != '' ? '/'.strtolower(url_title($category->url)) : $preffix;
    return site_url(lang('list_route').'/'.$category->id.$preffix);
}

I tried to do it this way:

public function post_list($category=FALSE,$category_name=FALSE) {
    if($category != FALSE) {
        $data['category_session'] = $this->blog->get_categories($category);
        $this->seo_tags->meta_title = (json_decode($data['category_session'][0]->metadata)->{lang('blog_language')} != '' ? json_decode($data['category_session'][0]->metadata)->{lang('blog_language')} : $data['category_session'][0]->title).' - '.$this->seo_tags->meta_title;

        if($this->uri_string != $this->blog->url_format_category($data['category_session'][0])) {
            redirect(base_url().$this->blog->url_format_category($data['category_session'][0]));
            exit;
        }
    }

But the url returns this way:

http://localhost/reweb/trunk/http://localhost/blog/trunk/novidades-e-estrategias-de-marketing-digital-em-nosso-blog/1/seo

And with this mistake:

Forbidden
You don’t have permission to access /blog/trunk/http://localhost/blog/trunk/novidades-e-estrategias-de-marketing-digital-in-our-blog/1/seo on this server.

  • 2

    A thousand options. Web server configuration files (Nginx, Apache etc.), Framework route configuration, specific route configuration to access the online-system... If you put the codes you have access to and are creating, we can make suggestions. I normally use the route and controller settings in the Framework.

  • @Fernandocordeiro I use Codeigniter. I have the routes specified in the Routes file. And that’s exactly what I need to do, from the route and controller settings.

  • 1

    I believe the most coherent is: if the url has some wrong data or even the parameter, which is directed to the 404 page in the function show_404(); Or redirect to a default route if you do not find your page (Slug, register) in the database if you have: $routes['novidades/(:any)'] = 'blog/padrao';

  • What your model in $this->blog->url_format($post) takes as parameter and which format of the returned string?

  • @GWER Please edit the question and add all this information instead of leaving it as comments... then delete the comments. This information is part of the issue.

  • I edited my question and entered more information. @Miguelangelo

Show 1 more comment

1 answer

2


It seems to me that $this->blog->url_format_category($data['category_session'][0])); is already returning the entire URL.

Try to change:

redirect(base_url(). $this->blog->url_format_category($data['category_session'][0]));

for

redirect($this->blog->url_format_category($data['category_session'][0]));

That is, just removing the base_url()

  • Giving a die() on this->blog->url_format_category($data['category_session'][0])really returned me the whole url, I withdrew the base_url() but now the page does not open, from the following error in the browser : A página da web em http://localhost/blog/trunk/novidades-e-estrategias-de-marketing-digital-em-nosso-blog/4/marketing-digital-imobilirio resultou em muitos redirecionamentos.

  • I decided as follows if(base_url().$this->uri->uri_string != $this->blog->url_format_category($data['category_session'][0])) {
 redirect($this->blog->url_format_category($data['category_session'][0]), 'location', '301');
 exit;
 }

  • 1

    I’m glad I helped you solve this stop, I’m even downloading the IC again to get practice on it again. I am adept at using var_dump() to see the exact values that are coming, they save me always... =)

  • 1

    Thanks man, saved me! I’m starting programming now, a really beginner mistake. Hehe

  • Well, you’re on the right track, asking, trying and doing =)

  • Thanks! Within 5 hours I already award the 50 bonus points (it’s the time I need to wait) :)

  • Problem-free =)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.