28

I am wondering wether it's possible to use .htaccess to rewrite a folder name. What I mean is this.

Lets say I have a url like:

www.site.com/folder1/page.php

Now I want to rewrite the url to (for example)

www.site.com/apple/page.php

The folder1 is an existing folder on my webspace.

important: the "apple" is not a folder rather just a name!

Ok here is a step by step plan:

  1. User types www.site.com/folder1/login.php
  2. The url should rewrite and not redirect the url to www.site.com/apple/login.php

This means that apple is just a name and not a directory. All the code should just come from folder1. Acutally apple should just be an alias for folder1. I can't just rename folder1 to Apple. Therefor I would just rewrite folder1 to apple.

5
  • does the apple folder exist on your webspace too?
    – Josh
    Aug 12, 2009 at 8:10
  • sorry you need to be clearer what you want to happen. can you give us a step by step what you want to happen eg what the url that the user types in and where the actual file is in your webspace.
    – Josh
    Aug 12, 2009 at 8:44
  • I updated I hope its more clear.
    – sanders
    Aug 12, 2009 at 9:03
  • In the first part, it seams that the "folder1" is hidden to the user which should see "apple" instead. But you "clarified" by adding "user types www.site.com/folder1/login.php"... Can you clarify... again...? Aug 12, 2009 at 9:17
  • when the user types www.site.com/folder1/login.php it should change the url to www.site.com/apple/login.php where apple is just an alias for folder1.
    – sanders
    Aug 12, 2009 at 9:28

2 Answers 2

60

mod_rewrite can only rewrite/redirect requested URIs. So you would need to request /apple/… to get it rewritten to a corresponding /folder1/….

Try this:

RewriteEngine on
RewriteRule ^apple/(.*) folder1/$1

This rule will rewrite every request that starts with the URI path /apple/… internally to /folder1/….


Edit    As you are actually looking for the other way round:

RewriteCond %{THE_REQUEST} ^GET\ /folder1/
RewriteRule ^folder1/(.*) /apple/$1 [L,R=301]

This rule is designed to work together with the other rule above. Requests of /folder1/… will be redirected externally to /apple/… and requests of /apple/… will then be rewritten internally back to /folder1/….

8
  • Ah, Gumbo I think you're right. I misunderstood what the original poster meant, it's described backwards. +1
    – zombat
    Aug 12, 2009 at 7:50
  • 1
    I tried this with site.com/folder1/login.php but it doesnt rewrite to site.com/apple/login.php
    – sanders
    Aug 12, 2009 at 7:58
  • @sanders: As I already said, you have to request /apple/login.php to have it rewritten to /folder1/login.php.
    – Gumbo
    Aug 12, 2009 at 8:15
  • 1
    @Gumbo: I want to rewrite folder1/login.php to apple/login.php and tried therefore RewriteRule ^folder1/(.*) apple/$1 but doesn't work. I even tried it with the indows regex tester but the patterns don't mactch
    – sanders
    Aug 12, 2009 at 8:22
  • 1
    With your edit it shows now www.site.com/apple/login.php but on the screen i see an error since it says that it can't find the requested page.
    – sanders
    Aug 12, 2009 at 9:21
5

try:

RewriteRule ^/apple(.*)?$ /folder1$1 [NC]

Where the folder you want to appear in the url is in the first part of the statement - this is what it will match against and the second part 'rewrites' it to your existing folder. the [NC] flag means that it will ignore case differences eg Apple/ will still forward.

See here for a tutorial: http://www.sitepoint.com/article/guide-url-rewriting/

There is also a nice test utility for windows you can download from here: http://www.helicontech.com/download/rxtest.zip Just to note for the tester you need to leave out the domain name - so the test would be against /folder1/login.php

to redirect from /folder1 to /apple try this:

RewriteRule ^/folder1(.*)?$ /apple$1 [R]

to redirect and then rewrite just combine the above in the htaccess file:

RewriteRule ^/folder1(.*)?$ /apple$1 [R]
RewriteRule ^/apple(.*)?$ /folder1$1 [NC]
4
  • thanks for your help but when i test it on this: I tried this with site.com/folder1/login.php but it doesnt rewrite to site.com/apple/login.php I get patterns not mach
    – sanders
    Aug 12, 2009 at 8:01
  • @Josh: your edit RewriteRule ^/folder1(.*)?$ /apple1$1 [R] doesn't work eiter.
    – sanders
    Aug 12, 2009 at 8:24
  • Are this redirects slower? when the redirect happens, comparing to a non redirect version?
    – Miguel
    Jan 14, 2016 at 10:48
  • 1
    unfortunately the test utility download link above no longer works
    – clayRay
    May 24, 2018 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.