3 Replies - 908 Views - Last Post: 24 October 2016 - 09:48 AM

#1 chris98  Icon User is offline

  • D.I.C Lover

Reputation: 40
  • View blog
  • Posts: 1,104
  • Joined: 06-July 13

.htaccess - Prevent file from inheriting Redirect rules from parent

Posted 21 October 2016 - 05:07 AM

Originally posted this on Stack Overflow yesterday but received no replies yet (I'm possibly a bit impatient, so sorry).

My site is structured like this:

include
include/interface/
include/templates/
include/subdomains/

include/subdomains/forum ...

I want to redirect the user to a 404 error when they view the fodler include and all subdirectories beyond this. This works very well, and the subdomains will view through the official forums.example.org but not through example.org/include/subdomains/forum/

To achieve this I use the rule:

Redirect 404 /include/

However, when I visit the forum and use rewritten URLs from the forum they give an error 404 because of how the forum is structured. The forum is structured like this:

include/rewrite.php

It's trying to require that file, but because of (what I'm assuming is the parent .htaccess file interfering with the forums htaccess file, it's not working and I'm getting a The requested URL .. cannot be found on this server error.

My question is, how can I keep the parent htaccess file redirecting the site's include folder to an error, but not the one for the forum?

My code is as below:

parent htaccess:
    RewriteEngine on
    # Do not remove this line, otherwise mod_rewrite rules will stop working
    RewriteBase /
    
    ErrorDocument 400 /errors/400
    ErrorDocument 401 /errors/401
    ErrorDocument 403 /errors/403
    ErrorDocument 404 /errors/404
    ErrorDocument 500 /errors/500
    ErrorDocument 501 /errors/501
    ErrorDocument 502 /errors/502
    ErrorDocument 503 /errors/503
    ErrorDocument 504 /errors/504
    ErrorDocument 505 /errors/505
    ErrorDocument 506 /errors/506
    ErrorDocument 507 /errors/507
    ErrorDocument 508 /errors/508
    ErrorDocument 509 /errors/509
    ErrorDocument 510 /errors/510
    
    AddDefaultCharset UTF-8
    
    <IfModule mod_headers.c>
      <FilesMatch "\.(js|css|xml|gz)$">
        Header append Vary: Accept-Encoding
      </FilesMatch>
    </IfModule>
    
    Redirect 404 /include/


Forum htaccess:
<IfModule mod_rewrite.c>

	# MultiViews INTERFERS WITH PROPER REWRITING.
	Options -MultiViews

	RewriteEngine On

	# UNCOMMENT AND PROPERLY SET THE RewriteBase IF THE REWRITE RULES ARE NOT WORKING PROPERLY.
	RewriteBase /

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule . include/rewrite.php [L]
    
    # ----------------------------------------------------------------------
    # DISABLE DIRECTORY BROWSING.
    # ----------------------------------------------------------------------
    <IfModule mod_autoindex.c>
        Options -Indexes
    </IfModule>

    # ----------------------------------------------------------------------
    # UTF-8 ENCODING.
    # ----------------------------------------------------------------------

    # USE UTF-8 ENCODING FOR ANYTHING SERVED text/plain OR text/html.
    AddDefaultCharset utf-8

    # FORCE UTF-8 FOR A NUMBER OF FILE FORMATS.
    AddCharset utf-8 .html .css .js .xml .json .rss

    # ----------------------------------------------------------------------
    # BETTER WEBSITE EXPERIENCE FOR INTERNET EXPLORER USERS.
    # ----------------------------------------------------------------------
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            BrowserMatch MSIE ie
            Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
        </IfModule>
    </IfModule>

    <IfModule mod_headers.c>
        # BECAUSE X-UA-Compatible ISN'T SENT TO NON-IE (TO SAVE HEADER BYTES), WE NEED TO INFORM PROXIES THAT CONTENT CHANGES BASED ON UA.
        Header append Vary User-Agent
        # CACHE CONTROL IS SET ONLY IF mod_headers IS ENABLED, SO THAT'S UNNECESSARY TO DECLARE.
    </IfModule>

</IfModule>



This post has been edited by chris98: 21 October 2016 - 05:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: .htaccess - Prevent file from inheriting Redirect rules from parent

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1995
  • View blog
  • Posts: 6,075
  • Joined: 15-January 14

Re: .htaccess - Prevent file from inheriting Redirect rules from parent

Posted 21 October 2016 - 11:37 AM

It's probably easiest to just specify a fully-qualified URL to redirect.

https://httpd.apache...s.html#redirect
Was This Post Helpful? 0
  • +
  • -

#3 chris98  Icon User is offline

  • D.I.C Lover

Reputation: 40
  • View blog
  • Posts: 1,104
  • Joined: 06-July 13

Re: .htaccess - Prevent file from inheriting Redirect rules from parent

Posted 24 October 2016 - 09:08 AM

Sorry for the slow response back to this.

I've tried all of the following things but only the original rule Redirect 404 /include/ seems to work. Was this the kind of thing you meant or have I misinterpreted it? I did look at the link as well which was how I came to try the code below for the internal server error.

Redirect 404 http://www.example.org/include/
Redirect 404 "http://www.example.org/include/"
Redirect 404 "/home/xxx/public_html/include/"
Redirect 404 /home/xxx/public_html/include/

Redirect http://www.example.org/errors/404 /include/



These rules bring up an internal server error:

<Location "/include">
    Redirect permanent "http://www.example.org/errors/404"
</Location>

<Location "/include">
    Redirect permanent "errors/404"
</Location>



Also for reference they were all added individually, not as a block.

** Edit **

Also just tried Options +Indexes in the forum htaccess and Options -Indexes in the parent but this had no effect as well. I've even gone so far as to place a blank .htaccess file in the include folder, removed the code from the public_html one and added this single line in the include/.htaccess file: Redirect 404 / - but this is causing the exact same problem ....

** Update **

Adding this in the forum .htaccess changes the error from 404 to 403: Options -FollowSymLinks

This post has been edited by chris98: 24 October 2016 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 chris98  Icon User is offline

  • D.I.C Lover

Reputation: 40
  • View blog
  • Posts: 1,104
  • Joined: 06-July 13

Re: .htaccess - Prevent file from inheriting Redirect rules from parent

Posted 24 October 2016 - 09:48 AM

Ok, I've now kind of got this working the way I intended. Through cPanel, I went to the "Index Manager", and disallowed access to the include directory.

This successfully prevents access to the include folder and all sub-folders from this, and it allows access to rewritten URLs on the forum.

However - it shows a '403 unauthorised' rather than a 404 which I would prefer if possible ..

** Edit **

And files are still accessible in the include directories.

** Update **

Fixed this problem with files being accessible by adding Deny from all to the htaccess file created in the 'include' directory from the Index Manager. This then prevented the forum rewritten URLs with the 403 forbidden error, so I added the line Allow from all in the forum .htaccess and this appears to have solved the problem for now.

Changed the error number in my main .htaccess file for the 403 error to 404, and it's now working exactly the way I want it to, but it's been very difficult to get it to work like this.

Thanks for the advice.

This post has been edited by chris98: 24 October 2016 - 10:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1