Yourls: No redirection when using "www"

Created on 12 Jan 2018  Â·  15Comments  Â·  Source: YOURLS/YOURLS

Hello;
Our short URLs are not properly redirecting when using "www".
I can access the home page with or without "www" but it returns back to home page if I use "www" with the shortned url.
Thanks for any feedback.

bug

Most helpful comment

We run 1.7.2 and were having this problem. I put a dirty fix in and wanted to share for anyone else. It fixes urls with www. in them but doesn't fix other subdomains.

In yours-loader.php, find the line below (~line 21)
$request = yourls_get_request();

After it, add:
$request = preg_replace('/^www./','',$request);

All 15 comments

Indeed, I can reproduce this.

Very vanilla install: http://sho.rt/ozh works but http://www.sho.rt/ozh doesn't (redirects to root), while both http://sho.rt/admin/ and http://www.sho.rt/admin/ are OK.

Just a clue...

After I made the last YOURLS_SITE patch, I tested the www vs no-www issue and both worked.

I then used server configuration (.htaccess) to make the following tests:

  1. allow www but disallow no-www
  2. disallow www but allow no-www
  3. allow www and allow no-www

Therefore, the 17 changes that the YOURLS_SITE patch made, may have touched on the problem.

As I said, this is _just a clue!_

@ozh - I have stopped working on the YOURLS_SITE patch. The more I worked on it, the more I became aware that it would be better to just move core constants into the DB and serve them as options through $ydb. Therefore, I am actively working on an admin config page for that purpose. This plus the full-fledged installer should solve this problem, the trailing slash problem, and the split YOURLS_SITE problem.

I am having the same issue. http://gbfam.us/nlamar will take you to the correct page, but www.gbfam.us/nlamar goes to the homepage (root). I found this issue posted before (#1080) but could not see any comments or solutions. Has anyone fixed this?

Apparently the new patch might have fixed this issue. I didn't get a chance to test it yet.

I am running 1.7.2 - is there something newer than that?

We run 1.7.2 and were having this problem. I put a dirty fix in and wanted to share for anyone else. It fixes urls with www. in them but doesn't fix other subdomains.

In yours-loader.php, find the line below (~line 21)
$request = yourls_get_request();

After it, add:
$request = preg_replace('/^www./','',$request);

Thanks, works!

Yep! I concur. Thanks @codegrrrl

YES! I've been pulling my hair over this, now it's fixed. Thanks @codegrrrl for your time and expertise 👍

Thanks @codegrrrl, tried many solutions but this fixed it :)

Thanks @codegrrrl, just what I needed ;)

I would like to know what is _properly redirecting when using "www"._ From the OP.

I'm on the home stretch of a PR refactoring the HTTP(S) scheme usage. This involves removing the YOURLS_SITE constant from config.php. So this topic is important during testing. What should a _very vanilla install_ do? I believe what Ozh reported HERE is the _correct_ behavior and this issue is _Not a Bug!_

  1. If YOURLS_SITE is defined as http://sho.rt does _[properly redirecting]_ include http://redirect.sho.rt, http://w3.sho.rt, or http://www.sho.rt?
    The domain sho.rt and the domain www.sho.rt are two different domains, suggesting the need for Multi-Domain Support #560, without hacking the core files.

  2. A simple, standard _301 redirect_ in .htaccess or the server block from www. to non-www. would do. This is used on many WordPress sites and is easy to do. Howbeit, it does add an extra 301.

# FROM www. --TO-- NO www. -- In .htaccess
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
  1. If the domain www. is a special case in YOURLS, what is the configuration for a site that does _NOT_ want to serve www. requests? _(Personally I want www. requests to be redirected to the non-www. "home page" where I have WordPress waiting to serve their needs.)_

  2. If you can delay a little, or temporarily use one if the above options. I expect to release the HTTP(S) refactoring PR and HTTPS Plugin in January 2019. Then all my effort will be directed on the YOURLS MultiSite Plugin. MultiSite is 99% done and working on my production server with three different domains. It can easily handle a configuration as the OP describes. Expect MultiSite Feburary 2019.

@ozh I believe the OP's issue is Not a Bug but a Multi-Domain Support #560 configuration error (see No. 1 above). What should a _very vanilla install_ do with two different domains? I believe what you reported HERE is the _correct_ behavior.

Social media sites are mangling non-www URLs. So if you say “site.net/nick” it’s showing that, but when someone clicks it’s sending them to http(s)://www.site.net/nick

This is an issue on twitter and facebook. The fix is to say “http(s)://site.net/nick” and not omit the http(s) portion OR — fix the site to accept the www portion if you have a dedicated domain for your shorturls (like many main sites both www and non-www resolve to the same/main site).

So I’m interested in whether this has been resolved in a graceful way or if it’s still pending. (I’m working on a cell phone right now with reading glasses in a shell app so it’s not like it’s a great time to read a ton of text lol — I may have to hack it and get back to updates and installations/fixes later).

i'm looking forward to a refactored yourls which does this "correctly." But for now, @codegrrrl 's "dirty" fix saved me!

Everybody: this issue has been fixed with latest commits.
Example: https://www.ozh.in/xt and https://ozh.in/xt both point here
(ozh.in is running current master version. I will probably release a new release sometimes soon)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LaughterOnWater picture LaughterOnWater  Â·  5Comments

ChrisChiera picture ChrisChiera  Â·  5Comments

uusijani picture uusijani  Â·  3Comments

Alan-Liang picture Alan-Liang  Â·  6Comments

ozh picture ozh  Â·  3Comments