Welcome to WebmasterWorld Guest from 54.158.86.243

Forum Moderators: Ocean10000 & incrediBILL & phranque

RewriteCond Site Redirect and Inheritance Issue

RewriteCond, redirect, inheritance

     
7:21 pm on Mar 14, 2017 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Feb 7, 2017
posts: 56
votes: 2


Here is my case of "I thought I had a clue" but the htaccess gods have deemed I do not. Please correct my understanding. Thanks.

I have 2 sites in subdirs under the host name "mysite.com", as well as some loose html pages, all under public_html:
public_html/site1/
public_html/site2/
public_html/oldstuff/index.htm this works
public_html/oldstuff/page2 this works
public_html/oldstuff/page3 this works
public_html/site3/ has a different hostname than mysite.com and is unaffected, this works

When I submit "mysite.com" or "mysite.com/site1/" it should go to mysite.com/site1/, and it does. When I submit "mysite.com/site2/" it should go to mysite.com/site2/, and it does. However I would like to RewriteOptions Inherit in the site2's htaccess, so I can inherit the RewriteCond of the parent in public_html, for the purpose of bot killing. When I turn site2's inheritance on and submit "mysite.com/site2/" it gets redirected back to site1.

I am confused because I think the regex ^www.mysite.com$ or ^mysite.com$ should not trigger with "mysite.com/site2/", and therefore should not redirect to "mysite.com/site1/"

My host is using Apache Version 2.2.29.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/mysite\.com\/site1\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^index.htm$ "http\:\/\/mysite\.com\/oldstuff\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^page2/?$ "http\:\/\/mysite\.com\/oldstuff\/page2\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^page3/index.htm$ "http\:\/\/mysite\.com\/oldstuff\/page3\/" [R=301,L]

Site2's htaccess addition for inheritance:
RewriteEngine on
RewriteOptions Inherit
...standard WP htaccess code...

When RewriteOptions Inherit is not turned on in the site2's htaccess I can still inherit using SetEnvIf and Deny from, and this works.
12:08 am on Mar 15, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13531
votes: 403


I suppose it's no use saying: Don't bother with "RewriteOptions inherit". It simply isn't worth the aggravation. Under Apache 2.2, "inherit" really means "If and only if no RewriteRules in the present htaccess apply to the current request, then go back and try the RewriteRules from the previous htaccess". (In 2.4 there are more options, but it's still not the same as "normal" mods with "normal" inheritance.)

You do not need to escape anything in the target. Ever.
You do not need to escape colons. Ever.
You do not need to ... Oh, all right.
You do not need to escape forward-slashes in mod_rewrite-- or, for that matter, anywhere else except where the slash is itself used as the RegEx delimiter (rare in Apache).
You do not need to put the target in quotation marks.

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^mysite.com$

Why, when you can achieve the same result with (using "example.org" as the name)
RewriteCond %{HTTP_HOST} example\.org
since you didn't say anything about subdomains that have to be excluded. Even if they existed, it would be a single line, with opening anchor only:
^(www\.)?example\.org

When RewriteOptions Inherit is not turned on in the site2's htaccess I can still inherit using SetEnvIf and Deny from, and this works.

Those are three different modules, each playing by their own rules. mod_setenvif inherits the way you'd expect things to inherit: it's cumulative. The same goes for mod_authz_thingummy (Deny/Allow directives).

Edit: What, if anything, is the line
RewriteCond %{HTTP_HOST} ^.*$
supposed to mean? Even without the anchors, it just says "something or nothing".
12:38 am on Mar 15, 2017 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Feb 7, 2017
posts: 56
votes: 2


Ok, if this inheritance is not worthwhile in Apache 2.2.9 then I'll leave it be. I could not get it to work anyway.

I've always been careful with my regex in my htaccess, so that is why I always escape special characters. I use an online regex tester, which highlights all errors. I did not know that you need not escape anything in the target!

I'll stick with SetEnvIf and deny froms: They work, they inherit and I understand them (now).

I'm unsure where I got this line: RewriteCond %{HTTP_HOST} ^.*$ I'll have to rethink it. As you say it means "something or nothing".

Thanks.
2:49 am on Mar 15, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13531
votes: 403


I did not know that you need not escape anything in the target!

Basically, the target isn't a Regular Expression (the same applies to any RegEx engine where you're replacing something with something else). It's literal text except for any captures-- $1 and %1 and so on.

Remember, RewriteRules don't have to have conditions at all.
3:26 am on Mar 15, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10747
votes: 43


IMPORTANT: Please Use Example.com For Domain Names in Posts:
https://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^mysite.com$

Why, when you can achieve the same result with (using "example.org" as the name)
RewriteCond %{HTTP_HOST} example\.org
since you didn't say anything about subdomains that have to be excluded. Even if they existed, it would be a single line, with opening anchor only:
^(www\.)?example\.org


actually:
public_html/site3/ has a different hostname than mysite.com...


in which case i would go with this:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
3:36 am on Mar 15, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10747
votes: 43


I did not know that you need not escape anything in the target!


RewriteRule Directive:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
Pattern is a perl compatible regular expression.

The Substitution of a rewrite rule is the string that replaces the original URL-path that was matched by Pattern.


it is worthwhile to (re)read the documentation on a regular basis.
i refer to the documentation early and often.
5:38 pm on Mar 15, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13531
votes: 403


i refer to the documentation early and often.

I've got the mod_rewrite docs bookmarked, because that's what I check most often--and from there I can navigate to any other mod or directive I might happen to be interested in. Currently it's a bit exasperating because I go to the 2.2 version and then you get the big warning message about this being a legacy release. (It's been, what, 2 or 3 years since they rolled out 2.4? It's not like those browsers that go from 0 to 50 in two months.) I'm starting to feel like those people whose hosts were on Apache 1.3 until the week before last.

Edit: "Vote early, vote often" is also a good guideline. Almost missed that. Oops.
6:06 pm on Mar 15, 2017 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Feb 7, 2017
posts: 56
votes: 2


I did not realize that my Apache 2.2.9 dates back to 2008. A2 should upgrade. V 2.2.32 is dated 2017-01-13.

V2.4 was released 2012-02-21. V2.4.25 released 2016-12-20 is the newest.
7:40 pm on Mar 15, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13531
votes: 403


V 2.2.32 is dated 2017-01-13.

Oh, now that's interesting. I had not realized they were still tweaking 2.2 even after releasing 2.4. Shades of mac System 6.0.8 (or was it 6.7.1?), which was created purely as a final tweak for people who couldn't move to System 7.

But, urk, 2012. That's a bit longer than a year or two.

:: casting dirty look at otherwise admirable host ::
1:12 am on Mar 16, 2017 (gmt 0)

Junior Member

Top Contributors Of The Month

joined:Feb 7, 2017
posts: 56
votes: 2


Friend on Planet Hoster brags that they are on V2.4.25. He did not even know it was the latest.