Skip to content

Instantly share code, notes, and snippets.

@ozh
Created April 19, 2022 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozh/714d23228235becba14fea925fc77e2c to your computer and use it in GitHub Desktop.
Save ozh/714d23228235becba14fea925fc77e2c to your computer and use it in GitHub Desktop.
YOURLS with IIS

Backup of probably obsolete https://github.com/YOURLS/YOURLS/wiki/Web-Config-IIS

What your web.config file for IIS should look like

Make a web.config file

If your YOURLS installation is on a IIS machine you have to create a web.config file. It's simple.

Case: YOURLS installed on root

If YOURLS root URL is http://yoursite/, the web.config file in the root directory must be like:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
        <rewrite>
            <rules>
                <rule name="YOURLS" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/yourls-loader.php" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Case: YOURLS installed in subdirectory

If YOURLS root URL is http://yoursite/somedir/, the web.config file in this subdirectory must be like:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
        <rewrite>
            <rules>
                <rule name="YOURLS" stopProcessing="true">
                    <match url="^somedir/(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/somedir/yourls-loader.php" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Bonus: Force non-WWW

Add this before the YOURLS rule block in your web.config file, replacing yourls.org with your own domain

<rule name="YOURLS www" stopProcessing="true">
    <match url="^(.*)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^www\.yourls\.org$" />
    </conditions>
    <action type="Redirect" url="http://yourls.org/{R:1}" redirectType="Permanent" />
</rule>

This directive MAY or MAY NOT work on your host (ie http://sho.rt/stuff might work fine and http://www.sho.rt/stuff might not). If it doesn't work, simply stick to one domain, either with or without www. There is no real support for both www and non-www domain setup.

(This is unlikely to get improved, to be honest. I don't get why people insist on adding 4 extra characters (www.) to their URL shortener...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment