.htaccess rewrite from subdirectory to root

21,607

How about something like this?

RewriteRule ^dev/(.*)$ $1

This would need to be in the root folder, or applied to the primary Apache configuration (not in .htaccess).

The usual rules about .htaccess redirects apply - including that RewriteEngine on, Options FollowSymLinks, and AllowOverride FileInfo are included somewhere. Full details at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule.

Share:
21,607
vicch
Author by

vicch

Updated on July 09, 2022

Comments

  • vicch
    vicch almost 2 years

    I have some difficulties with the mod_rewrite rules. I want to rewrite any request on

    www.example.com/dev/*
    

    to

    www.example.com/*
    

    For example when the request url is www.example.com/dev/index.php, the response should be www.example.com/dev/index.php, and not just that the url looks like it's from the root directory, but it is actually using the index.php from root directory.

    I have tried the mod_alias which works

    RedirectMatch (^/dev/)(.*) http://www.example.com/$2
    

    But it is not possible to apply ip address conditions with mod_alias, so I still need a solution with mod_rewrite.

    Would anyone share some knowledge please? Thanks.

  • vicch
    vicch over 12 years
    No, not any luck. I wonder if it has anything to do with the other rules which are used to prevent access to certain types of files. So I put this new rule in the first line just below RewriteBase /, but still not working.
  • Ulrich Palha
    Ulrich Palha over 12 years
    @vicch Try adding a [L] flag i.e change the rule to RewriteRule ^dev/(.*)$ $1 [L] so it does not process other rules
  • ziesemer
    ziesemer over 12 years
    @vicch - You need to simplify your configuration for working this issue. Remove all your other rules and test just this, then start adding other rules back in until you find what is interfering with this.
  • vicch
    vicch over 12 years
    OK, the problem is solved. It was the .htaccess in the dev folder that prevents it from working. So I just disabled it for the redirection to work. Thanks, ziesemer and Ulrich.
  • vicch
    vicch over 12 years
    Strangely, though, the url displayed in browser is dev/index.php while the file used is actually index.php from root folder.
  • ziesemer
    ziesemer over 12 years
    Yes, per your original question: "the response should be www.example.com/dev/index.php". To eliminate the /dev/, add [R] to the end of it to force a redirect. (It's not as efficient for the browser, but it will get you your desired result.)