How can WordPress and YOURLS peacefully coexist at the root level?

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.

Read More

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

Related posts

5 comments

  1. 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.

  2. 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 if blah 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 URL
    • blah is a YOURLS short URL but not an WP article
    • blah is neither a WP article or a YOURLS short URL
    • optionally decide what to do when blah is both a WP article and a YOURLS short URL

    Your code would also need to :

    • prevent WP to create a “slug” that is already a YOURLS short URL
    • prevent YOURLS to create a short URL that already matches a WP slug

    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.

  3. 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

    # BEGIN YOURLS
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^.*$ /yourls-loader.php [L]
        RewriteRule ^$ /w [L]  
    </IfModule>
    # END YOURLS
    

    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)

  4. 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:

    1. On your htaccess, keep it simple, as wordpress wants it to be. Remove the snippet htaccess for the YOURLS swap plugin, if you have it added.

    2. Edit your 404.php page theme. Mine was at: wp-content/themes/twentynine/404.php

    At the very begin, just add:

    <?php 
    header('Location: http://sub.domain' . $_SERVER['REQUEST_URI'] );
    exit;
    ?>
    

    This will allow:

    1. All files and directories will honor the basic htaccess from which they are simply served
    2. All URLS not files and folders will be handled by wordpress in the usual manner.
    3. If a page is not found, wordpress will trigger 404.php which will send that page to YOURLS to try
    4. If YOURLS also don’t find it, a 404 page from YOURLS will show.

    Remember 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

Comments are closed.