Forum Moderators: phranque

Message Too Old, No Replies

.htaccess rewrite not working if subdir also contains .htaccess file.

         

SteveS1951

3:34 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



I am having trouble with a redirect in my top level domain if a subdirectory also contains a .htaccess file.

To help explain, here is my directory structure.

www.domain1.com points to directory /public_html

www.domain2.com points to directory /public_html/domain2

My goal is to keep people from directly accessing directory domain2 using
http://www.domain1.com/domain2/
I want them to access it only by
http://www.domain2.com/


Here is the .htaccess file that I have placed in directory /public_html

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain1.com$
RewriteRule ^domain2\/?(.*)$ - [F]

If I do not have a .htaccess file in directory /public_html/domain2, then this works fine and properly displays the Forbidden error.

In directory /public_html/domain2 I also have the following .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain2.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.com$
RewriteRule ^forums\/?(.*)$ "http\:\/\/forums\.domain2\.com\/$1" [R=301,L]


because I want users to be sent to
http://forums.domain2.com
when they enter
http://www.domain2.com/forums/
(this redirection works fine)

The problem I am encountering is when directory /public_html/domain2 contains a .htaccess file, then the rewrite rule in the .htaccess file in directory /public_html for domain www.domain1.com does not work.

Any assistance will be apperciated.

Steve

g1smd

6:09 pm on Sep 8, 2011 (gmt 0)

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



The code you have a huge amount of syntax errors (missing escaping on periods, unwanted escaping of slashes, etc) and several logic errors.

What you probably need in the root of domain1 is:

RewriteRule ^domain2/(.*) http://www.example.com/$1 [R=301,L]

SteveS1951

7:19 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



I have very little experience with .htaccess. Are you aware of any on-line analyzers to check syntax of .htaccess code. Also can you recommend a good tutorial (a few thousand are returned from a Google search).

I must have even less of an understanding than I thought. In your example

RewriteRule ^domain2/(.*) http://www.example.com/$1 [R=301,L]


I would have placed a $ after the (.*) because I thought that the dollar sign ($) signifies the end of the string to be matched. Does a space also signify the end of the string to be matched?

lucy24

9:04 pm on Sep 8, 2011 (gmt 0)

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



The space is a syntactical element in htaccess. It's the divider between each element. Or, as [httpd.apache.org...] puts it:

RewriteRule Pattern Substitution [flags]

That's why literal spaces have to be \ escaped in htaccess. Here, ^domain2/(.*) is the "pattern"; http://www.example.com/$1 is the "substitution"; and the part in brackets (comma-delimited if necessary) is the "flag".

You don't need a $ because Regular Expressions are greedy by default and will grab everything they can until they run out of things to grab. So you only need anchors if some specific text has to occur at the beginning or end.

g1smd

11:20 pm on Sep 9, 2011 (gmt 0)

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



^domain2/(.*)
is

"begins with literal 'domain2' - followed by literal slash - followed by something, anything, everything or nothing; but whatever it is capture it for re-use in $1."