I want to make a better link shortener service using YOURLS. I’ve procured a short URL for this: sly.pe. YOURLS is installed and sly.pe/admin is accessible.
I’d also like to use WordPress to power most of the consumer facing site, at the URL: sly.pe. The idea being I want users to register for the site using WordPress and a registration plugin like OneAll social. Once authenticated they get logged in for both YOURLS and WordPress and are redirected to a URL like sly.pe/home that uses WordPress as the site but YOURLS (sly.pe/admin for that user) is embedded and accessible somehow.
Both services make .htaccess changes. If I leave the file as is, I can’t seem to have any other WordPress URL other than sly.pe, but YOURLS works fine. If I comment out some YOURLS code, WordPress can use other directories, but then YOURLS doesn’t work. How can I have both coexist nicely? Or at least define the WordPress URLs I want and get those working?
(commenting out the YOURLS code gets WordPress working, but breaks YOURLS):
.htaccess
:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I was able to get YOURLS to work with WordPress. Here’s how I did it…
I’ve placed YOURLS in niemannross.com/links – so for me, a redirect looks like http://niemannross.com/link/rlunchlessons
Install the Redirection plugin by John Godley in your copy of WordPress.
Look in WordPress:Admin:Tools:Redirection. Across the top are several tabs. You should see Redirects | Groups | Logs | 404s | Import/Export | Options | Support.
Choose Redirects
Select Add New Redirection
Source URL: /link/.* – Select Regex.
Target URL: /link/yourls-loader.php
Group: Whatever makes sense in your setup. I’ve chosen wordpress:redirections
This works for my installation.
Simple answer is: you cannot. Both WordPress and YOURLS (please, YOURLS, not YourLS) need to process server requests to return the expected result (eg when requesting
http://sho.rt/blah
, each app needs to check ifblah
is content it manages)Long answer: you should not because all of the following.
Basically you would need something that works when:
blah
is an WP article but not a YOURLS short URLblah
is a YOURLS short URL but not an WP articleblah
is neither a WP article or a YOURLS short URLblah
is both a WP article and a YOURLS short URLYour code would also need to :
You’d need to have code for this both in the WP area and in the YOURLS area.
Bottom line: lots of edge cases to deal with. What might work in some case on your side might not work in other cases, on your side or with others.
I think you’re going to have to work around the problem as mentioned by @Ozh, but here’s a plugin that will make it easier for your WordPress users to use YOURLS: https://wordpress.org/plugins/yourls-link-creator/
You cannot, I tried different ways, maybe with new apache 2.4 using the
if else
statement you will, but I am not an expert on those commands, I want to share what I did to have the shortener option in the root page and the html site in a subdirectory, with this solution you can still give people normal url for the website (e.g. sho.rt):set your website in a folder under root with a name e.g. “w“, that means it will be at “sho.rt/w”
then use this .htaccess
This will give you the option to still use sho.rt as your website address if the user doesn’t pass any subfolder in the url, but if they do, it will check in yourls first and go to your folders if nothing found.
One more thing, do not forget to add “w” in the $yourls_reserved_URL array in /user/config.php (Yourls config file)
I finally solved this for my website.
I have a subdomain http://sub.domain where I installed YOURLS, but the short URLs are at http://domain/someshortlink
And the wordpress website is also at http://domain/
It used to work with the swap plugin ( https://github.com/gerbz/Yourls-Swap-Short-Url ).
Just with the
htaccess
provided by that YOURLS plugin, and no wordpress. But since I decided to move to wordpress, this combination won’t work.Here is the trick:
On your
htaccess
, keep it simple, as wordpress wants it to be. Remove the snippethtaccess
for the YOURLS swap plugin, if you have it added.Edit your
404.php
page theme. Mine was at:wp-content/themes/twentynine/404.php
At the very begin, just add:
This will allow:
htaccess
from which they are simply served404.php
which will send that page to YOURLS to tryRemember to edit the 404 page from YOURLS also, to match your wordpress theme.
Best of luck!
Reference: https://github.com/gerbz/Yourls-Swap-Short-Url/issues/11