Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

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]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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