Forum Moderators: phranque

Message Too Old, No Replies

Permanent Redirect Except for Certain Directories

.htaccess redirect help

         

angmi90

7:31 pm on Jul 27, 2007 (gmt 0)

10+ Year Member



I recently moved my website from one domain to another and to forward the links from the old site to the new site, I used the following in my .htaccess...

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

However, I still want the links to forward to the new domain, but I want to make it so a few directories don't forward. For example, when I type in www.OLDDOMAIN.com/information -- I want it to go to the old domain, not the new domain.

How can I do this? Thanks.

[edited by: jdMorgan at 3:46 am (utc) on July 28, 2007]
[edit reason] example.com [/edit]

jdMorgan

3:45 am on Jul 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add a RewriteCond to the rule to make it conditional:

Options +FollowSymLinks
RewriteEngine on
# Exclude /information directory from redirect
RewriteCond $1 !^information
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

--or, more generally--

Options +FollowSymLinks
RewriteEngine on
# Exclude /information directory from redirect
RewriteCond %{REQUEST_URI} !^/information
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

sun818

5:31 am on Jul 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi Jim - I understand multiple conditionals can be used. Does it matter the order in which conditionals are listed? If I have multiple directories that I do not want redirected, should I list directories from most frequently requested to least frequent? My point in asking is to use the least server resources to accomplish the task.

jdMorgan

7:40 pm on Jul 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, listing them in most-to-least-accessed order will improve performance for those directories, since the processing for the rule can "quit" earlier. However, RewriteCond order won't help for any other directories (those not listed), since all the exclusions will have to be tested (and rejected), and then the rule will execute.

You can also use the "local OR" construct instead of multiple RewriteConds, that is:


RewriteCond %{REQUEST_URI} !^/(dir1¦dir2¦dir3)

instead of

RewriteCond %{REQUEST_URI} !^/dir1
RewriteCond %{REQUEST_URI} !^/dir2
RewriteCond %{REQUEST_URI} !^/dir3

Interestingly, the local OR is faster in .htaccess, while the individual RewriteConds are faster in httpd.conf, conf.d, etc. This is because httpd.conf and conf.d are pre-compiled, while .htaccess is interpreted on-the-fly for each HTTP request.

Be sure to change the broken pipe "¦" characters above to solid pipes before use; Posting on this forum modifies the pipe characters.

Jim