1

I had some .htaccess lines that worked for transparently rewriting my www subdomain to a www subfolder. Now, since a server update, it doesn't work anymore.
Actually, it does still work, except for root without request (number 1 below), which gives a 404 now. What's going on? Doesn't !^/www include empty requests?

This is what I want and worked before:

  1. http://www.e-motiv.net -> subfolder: www
    doesn't work anymore
  2. http://www.e-motiv.net/anything -> subfolder: www/anything
  3. http://e-motiv.net/www/(anything) -> don't rewrite

These are my htaccess lines:

#### www -> folder /www (not possible via Control Panel)
RewriteCond %{HTTP_HOST} ^www\.e-motiv\.net$ [NC] 
RewriteCond %{REQUEST_URI} !^/www
RewriteRule ^ /www%{REQUEST_URI} [L]
#RewriteRule ^(.*)$ /www/$1 [L]  #Variant

#check https://stackoverflow.com/questions/9083514
DirectorySlash Off
#www dir only
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^www/(.+[^/])$ /$1/ [L]
#other dirs
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^(.+[^/])$ /$1/ [L]
####

EDIT: I discoverd if I add R in fourth line, it works again, except that everyone can see it now, which I don't want. I am wondering now if it's not related to the bug said in Internal URL rewrite no longer working after upgrading Apache to 2.4
EDIT: Third case rewritten from "http://e-motiv.net/www" to "http://e-motiv.net/www/" as it makes no sense without slash.
EDIT: ALthough there is a workaround below for my case, the best thing to do is update your apache! My hoster couldn't, so for those who are in the same situation, it's still meaningfull (and instructive).

Community
  • 1
  • 1
e-motiv
  • 5,795
  • 5
  • 27
  • 28
  • Does `/www/` folder also have .htaccess? – anubhava Jul 20 '15 at 16:06
  • 1
    Nope. Also, I discoverd if I add R in fourth line, it works again, except that everyone can see it now, which i don't want. I am wondering if it's not related to the bug said in http://stackoverflow.com/questions/20023601/internal-url-rewrite-no-longer-working-after-upgrading-apache-to-2-4 – e-motiv Jul 20 '15 at 16:14
  • Adding the following lines are a workaround to my problem: RewriteCond %{HTTP_HOST} ^www\.e-motiv\.net$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ www/index.php [QSA,L] – e-motiv Jul 20 '15 at 16:42

1 Answers1

1

This rule has worked for me in Apache 2.4:

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.e-motiv\.net$ [NC]
RewriteRule ^/?$ www/ [L]

RewriteCond %{HTTP_HOST} ^www\.e-motiv\.net$ [NC]
RewriteRule ^((?!www/).*)$ www/$1 [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Exact same problem with those lines. Which apache version do you have? – e-motiv Jul 20 '15 at 16:32
  • 1
    I have **Apache/2.4.12** – anubhava Jul 20 '15 at 16:36
  • In the bug I referenced, they say it's only fixed in 2.4.9. Since you have a higher version, it seems to point to me experiencing that bug after all. – e-motiv Jul 20 '15 at 17:55
  • 1
    Hmm, can you test my updated rules on your Apache 2.4.9 – anubhava Jul 20 '15 at 18:26
  • That seems to work better, except: http://e-motiv.net/www. That one shows the html, but not the css, although the css can be found where it should: http://e-motiv.net/www/css/general.css. But then again, http://e-motiv.net/www/ (notice the "/") DOES work. – e-motiv Jul 25 '15 at 18:00
  • It is because your page is using css as ` – anubhava Jul 26 '15 at 04:40
  • You should also add this in the `` section of your page's HTML: `` so that every **relative URL** is resolved from that base URL and not from the current page's URL. – anubhava Jul 26 '15 at 04:40
  • You're mistaken on that base tag. Since it has to work with all the variants in my original post it has to be relative (with and without www folder). Also, a web developer should not have to set the bas tag for this case. You're probably right on that last rule though. But I am gonna do without the third variant. If peoplee still reach my site with www at the end, I will notice in my stats. It was a bad idea of mine to keep it running (especially if there is no slash at the end. I will update post too.) – e-motiv Jul 26 '15 at 13:57
  • It's wrong, because it wouldn't work for all cases in my question. (There is no www folder in www.e-motiv.net :-) ). Anyway, the problem is futile now -> "gonna do without the third variant" (without slash). P.S. Grats on working around that apache bug. There are a lot of stackoverflow posts with it that you might wanna get some points from. ;-) – e-motiv Jul 26 '15 at 14:04
  • 1
    I would sincerely advise them all to upgrade if at all it is possible :) – anubhava Jul 26 '15 at 14:06
  • 1
    Yeah. I know, that's the best advise. I advised my hoster, who's a comrade of mine, too about that, but he won't. Probably too much work already done in transferring zillion of his customers... – e-motiv Jul 26 '15 at 14:07