Htaccess - silent redirection from one directory to another

I’m newbie with apache configuration. I try to achieve the functionality in which the user type in browser: mysite.com/projects and is silently redirected to the directory : mysite.com/projects/sub1/sub2/index.html.

My htaccess file residing within the root directory:

RewriteEngine on
RewriteRule ^projekty/(.*)$ http://3d-webstudio.com/projects/sub1/sub2/index.html$1 [R=301,L]

Unfortunately with the above code the user sees the redirection. So what am I doing wrong?

Regards

The user sees it because you’re doing an external redirect (that is, sending back an HTTP 3xx response) rather than an internal rewrite. You’ll need to get rid of the R flag as well as the absolute URL.

RewriteRule ^projekty/(.*)$ [COLOR=“#FF0000”]http://3d-webstudio.com/[/COLOR]projects/sub1/sub2/index.html$1 [R=301,L]

…becomes…

RewriteRule ^projekty/(.*)$ projects/sub1/sub2/index.html$1 [L]

Also, there may be another problem. In your example URL, you have “projects” but in your rewrite you have “projekty”. Assuming the latter is a typo and is supposed to be “projects”, the problem is that the pattern ^projects/(.*)$ will match every URL under projects/, including the URL you’re trying to rewrite to – projects/sub1/sub2/index.html – so you’ll end up in a rewrite loop.

To solve this, we’re going to have to get more specific about what you actually intend to match. For example, if you intend for only the one url projects/ to be rewriten, then you could write it this way.

RewriteRule ^projects/$ projects/sub1/sub2/index.html$1 [L]

Or, if instead you intend to match all sub-paths of projects/, then you’ll have to filter out your new URL path.

RewriteCond $1 !^sub1/sub2/
RewriteRule ^projects/(.*)$ projects/sub1/sub2/index.html$1 [L]

Thanks for the reply. It’s very helpfull.

The code:

RewriteRule ^projects/$ projects/sub1/sub2/index.html$1 [L]

represents functionality that I’ve been looking for. But I’ve noticed, that index.html stopped working. Only white page shows up (as if index.html didn’t have any content). What does it mean?

Ahh, my fault. In that new line of code, now that we’re not capturing anything, the $1 variable will not be set, so we need to remove it.

RewriteRule ^projects/$ projects/sub1/sub2/index.html$1 [L]

…becomes…

RewriteRule ^projects/$ projects/sub1/sub2/index.html [L]