Forum Topic: WordPress and Yourls

Forum: .htaccess Forum : WordPress • Posted by Smith • Updated:

I noticed this site uses both WordPress and Yourls (for short URLs) in the same directory.. the official Yourls documentation says it isn’t possible:

YOURLS cannot share its root directory with another .htaccess rewrite rules driven app such as WordPress. The .htaccess file should contain YOURLS directives and no other rewrite rule.

Is there a clean way to do it with .htaccess?

3 Replies to “WordPress and Yourls”

Posted by Jeff Starr

Well, maybe not “clean”, but it is possible using mod_rewrite and regular expressions, which will depend on how you’ve got things setup with both WordPress and Yourls.

For this site, all WP-generated URLs are longer than 3 characters, and all Yourls URLs are shorter than 3 characters. So matching 3 or less characters is the key, and possible using the following regex:

^/([a-zA-Z0-9]{1}|[a-zA-Z0-9]{2}|[a-zA-Z0-9]{3})/?$

To integrate this into the root .htaccess file, we exclude matching requests from the WP rules and require the match for the Yourls rules. Here is the complete code:

<IfModule mod_rewrite.c>
	# WP
	RewriteBase /
	RewriteRule ^index\.php$ - [L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9]{1}|[a-zA-Z0-9]{2})/?$ [NC]
	RewriteRule . /index.php [L]

	# YOURLS
	RewriteBase /
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]{1}|[a-zA-Z0-9]{2})/?$ [NC]
	RewriteRule .* /yourls-loader.php [L]
</IfModule>

That works a treat for me, but more elaborate pattern-matching may be required depending on your setup and goals.

Posted by eet

Is it necessary to include RewriteBase / more than once?

Posted by Jeff Starr

No it’s not, but useful for keeping things organized. I like to keep rulesets as modular as possible by keeping all involved directives grouped together (in most cases).