0

I have a primary site running ExpressionEngine and I am trying to get YOURLS running in a subfolder called "work", ie: http://foo.com/work/ for short urls and EE is running at the root, ie: http://foo.com/. Please note there is no 'www'.

The problem I am having is that when I use a short URL and a user adds "www" to the URI, such as http://www.foo.com/work/123 I get a redirect chain that looks like this:

Everything works fine if you omit the 'www' from the URI. YOURLS is set up to use the non-www but the worry is that some users might habitually type the 'www' as these URIs are being placed on printed ads.

The root .htaccess file looks like this:

SetOutputFilter DEFLATE
DirectoryIndex index.php index.html

# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteCond %{HTTP_HOST} ^www.foo.com [NC]
RewriteRule ^(.*)$ http://foo.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
</IfModule>
# END ExpressionEngine Rewrite

The /work/ (YOURLS) .htaccess looks like this:

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

How can I get the 'www' URIs to work like the non-www URIs do without completely hosing EE? :)

zessx
  • 68,042
  • 28
  • 135
  • 158
Marty
  • 2,606
  • 7
  • 29
  • 35
  • 1
    Try adding the 2 lines after `RewriteBase...` in the root .htaccess, to the `/work` .htaccess at the same position. (After `RewriteBase..`). – Felipe Alameda A Mar 05 '13 at 15:47
  • I edited and added these lines as you said, and now it does 301 redirect but to http://foo.com/123 from http://foo.com/work/123 | The lines I added are RewriteCond %{HTTP_HOST} ^www.foo.com$ [NC] RewriteRule ^(.*)$ http://foo.com/work/$1 [L,R=301] – Marty Mar 05 '13 at 16:04
  • 1
    Last rewrite rule in your comment is different (`RewriteRule ^(.*)$ foo.com/work/$1`). Should be exactly the same: `RewriteRule ^(.*)$ http://foo.com/$1 [L,R=301]`. – Felipe Alameda A Mar 05 '13 at 16:07
  • If still doesn't work as expected, try putting the 2 lines before the `RewriteBase` directive. – Felipe Alameda A Mar 05 '13 at 16:21
  • That did it, thanks! Leave it as an answer below and I'll give you credit. – Marty Mar 05 '13 at 17:51

1 Answers1

0

I faced a similar issue. Some end users or people publishing the short urls tend to append them with www while the short url was bought with the intention of using it without www.

I also installed the YOURLS redirect index plugin to forward homepage visitors to our regular homepage.

So I had a few different types of urls:

  1. short domain homepage, should lead to the regular https homepage
  2. short domain YOURLS admin panel, should lead to the https admin panel
  3. short domain shortened urls, should lead to the long url

I wanted all urls to work with or without www and make sure http was forwarded to https.

My solution was to start .htaccess with these lines:

# Force HTTPS (because of passwords via /admin) and use clean domain (non www)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.examp.le [NC]
RewriteRule ^(.*)$ https://examp.le/$1 [L,R=301]
Mackaaij
  • 470
  • 2
  • 9