I've searched and tried everything I've found and nothing seems to work quite right. What I'm trying to do is:

  • redirect all pages from a domain (domain.com) to a directory within the same domain (domain.com/dir or domain.com/dir/index.php);

However, I do not want to redirect:

  • domain.com/index.php;
  • domain.com;
  • www.domain.com
  • and, obviously the directory to which all the pages will redirect to (domain.com/dir).

I'd like to use .htaccess to do this since it seems to be the most obvious choice–I'm just not as familiar with .htaccess scripting as I would like to be. Any help would be much appreciated. Thanks so much in advance!


First off, don't use .htaccess:

in general, use of .htaccess files should be avoided when possible. Any configuration that you would consider putting in a .htaccess file, can just as effectively be made in a section in your main server configuration file.

Put this in your <VirtualHost>:

RewriteEngine On
RewriteRule !^(/|/index\..+|/dir/.*)$ /dir%{REQUEST_URI} [R=301,L]

Or, if you insist on using .htaccess even after reviewing the Apache documentation on the matter, then you'll need to strip the leading slash from the match and replace strings:

RewriteEngine On
RewriteRule !^(|index\..+|dir/.*)$ dir%{REQUEST_URI} [R=301,L]

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.