invisible mod_rewrite no siempre es invisible !? ("www" y "sin subdominio")

My site is on a host using cPanel 11.

Unfortunatly it redirects both "" and "" to public_html. I want resp. public_html/www and public_html/ and this invisible to the end user.

I thought the best way was through mod_rewrite, so I did the following.

File space looks like this (from public_html/):

  • /.htaccess
  • /index.php
  • /www/index.html
  • /www/test/index.html

And I want this (second part invisible!):

  1. -> /index.php
  2. -> /www/index.php
  3. -> /www/test/index.php

I thought this would do it:

RewriteCond %{HTTP_HOST} ^$
RewriteCond %{REQUEST_URI} !/www
RewriteRule ^(.*)$ /www/$1  [NC,L]

1 and 2 work, but although 3 gives the right file, it changes the address!? (so not invisible) So, in address bar you get: -> Huh??

If mod_rewrite is not the best solution, please do tell!

preguntado el 31 de enero de 12 a las 16:01

1 Respuestas

This is because of mod_dir. mod_dir adds the tailing slashed to urls that map to directories. mod_dir is not aware of these 'virtual urls' created with mod_rewrite.

So either disable this behavior by using

DirectorySlash Off

This will however make requests to result in a 404 not found. You can fix this with some rewriterule though. So the complete solution would be something like:

DirectorySlash Off
#www dir only
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^www/(.+[^/])$ /$1/ [R,L]
#other dirs
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^(.+[^/])$ /$1/ [R,L]

Respondido el 31 de enero de 12 a las 22:01

Thank you! Deep knowledge there! Could you remove "R" flags? Though it's invisible, it sends a rederict to the browser. Where might that lead to (SEO, script-loops,..)? Also a side problem. I have a custom "ErrorDocument 404 /404.php", and works generally, but e.g. "" defaults to server 404. Is there an easy way to handle that in the current htaccess file? Both things are not in my original question, but could you favor me? And any comments on "best solution"? Now there are actually 8 instructions. Is this still the best way to handle the problem at base? - e-motiv

You could remove the R flag, but I would not advise this. If you consider SEO you MUST keep the R flag, to prevent duplicated content. There is no "better" solution to this problem. Also, 8 rules is nothing to worry about. As to the 404 problem; I'm not entirely sure, but I would suggest copying the 404.php to the /www folder. Otherwise I need some more info as to what urls work and what don't. - Gerben

Well, the whole deal is actually about not-duplicated content (www en non-www serve different sites), but well, I hope other readers can differentiate it in these comments then. Thanks for your opinion on the 8 rules! 404: only those non-existing files (or folders with non-index files; see my example in first comment) beyond folder "www" don't go to my custom 404 . I'll try what you suggest. If you know more, I appreciate any more comments! Thanks again! - e-motiv

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.