SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    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/(.*)$ http://3d-webstudio.com/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]
    "First make it work. Then make it better."

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by norbi5 View Post
    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]
    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •