ash-jc-allen / short-url

A Laravel package for creating shortened URLs for your web apps.
MIT License
1.25k stars 157 forks source link

Editing existing links donยดt work #204

Closed hardlevel closed 1 year ago

hardlevel commented 1 year ago

Hi! How are you? Fine I hope!

I made a little sytem to generate short urls using your lib, it's amazing.

But I'm facing a problem, I can't edit existing links. It shows the edited value, I can see it on database, but when I try to access the shortened url, it goes to the old destination. There's something else that I need to do?

This is my update function:

    public function update(Request $request, ShortURL $link)
    {      
        //dd($request);
        $link = ShortURL::find($request->id);
        $link->destination_url = $request->old_link;
        $link->url_key = $request->alias;
        $link->default_short_url = $request->new_link;
        $link->update();

        // redirect
        //Session::flash('message', 'Link atualizado!');
        return back();
    }

Thanks!

ash-jc-allen commented 1 year ago

Hey! I'm doing well thanks and hope you are too? ๐Ÿ˜„

Sorry that you're having issues with the package. Hopefully, we can figure out what's going on with it! I have a hunch that your issue might be similar to #199 in that you're creating your short URLs using a 301 redirect status code.

Just to copy some info over from the other issue:

By default, the short URLs that are created are given a 301 status code. So the first time you visit your short URL, you will hit your Laravel app and be redirected to the destination URL. But the browser will remember this destination URL, so next time you try and visit the short URL, your browser will redirect you straight to the destination URL. This means you'll never hit your Laravel app after the first time you visit it (in the same browser).

First visit:

Short URL -> Laravel app -> Destination URL

Second, third, forth, etc visit:

Short URL -> Destination URL

If you'd like to hit your Laravel app on every request, you could set your redirect status code to be 302 instead (https://github.com/ash-jc-allen/short-url#redirect-status-code).

My guess is that your short URLs are being created with a 301 status code, so your web browser isn't visiting your Laravel app again to check if the URL changed.

Would you be able to confirm whether this is the case? ๐Ÿ™‚

hardlevel commented 1 year ago

Hi, thanks for your reply

So I can change it to code 302, but what I have to do with the links that is already created?

Em qua., 19 de jul. de 2023 ร s 10:36, Ash Allen @.***> escreveu:

Hey! I'm doing well thanks and hope you are too? ๐Ÿ˜„

Sorry that you're having issues with the package. Hopefully, we can figure out what's going on with it! I have a hunch that your issue might be similar to #199 https://github.com/ash-jc-allen/short-url/issues/199 in that you're creating your short URLs using a 301 redirect status code.

Just to copy some info over from the other issue:

By default, the short URLs that are created are given a 301 status code. So the first time you visit your short URL, you will hit your Laravel app and be redirected to the destination URL. But the browser will remember this destination URL, so next time you try and visit the short URL, your browser will redirect you straight to the destination URL. This means you'll never hit your Laravel app after the first time you visit it (in the same browser).

First visit:

Short URL -> Laravel app -> Destination URL

Second, third, forth, etc visit:

Short URL -> Destination URL

If you'd like to hit your Laravel app on every request, you could set your redirect status code to be 302 instead ( https://github.com/ash-jc-allen/short-url#redirect-status-code).

My guess is that your short URLs are being created with a 301 status code, so your web browser isn't visiting your Laravel app again to check if the URL changed.

Would you be able to confirm whether this is the case? ๐Ÿ™‚

โ€” Reply to this email directly, view it on GitHub https://github.com/ash-jc-allen/short-url/issues/204#issuecomment-1642098762, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFROV2OLQWGPVLQL7LW7GFLXQ7PE5ANCNFSM6AAAAAA2P4FD6Q . You are receiving this because you authored the thread.Message ID: @.***>

ash-jc-allen commented 1 year ago

As far as I'm aware, if a user has already visited a link that had a 301 redirect code, they'll need to clear their browser's cache (I think). Otherwise, their browser will permanently redirect to the old destination URL that they were originally redirected to ๐Ÿ™‚

hardlevel commented 1 year ago

this is not a problem, the links are not public yet...

but I have to do something with the existing links to support editing?

ash-jc-allen commented 1 year ago

Without looking at your project, it's difficult to know what you might need to do to support editing. But I imagine that you'll probably need to update all your existing links in the database to use a 302 redirect status code by updating the values in the redirect_status_code column in the short_urls table ๐Ÿ™‚