1

I have a site that evaluates any URL against their database and redirects accordingly to a php content or a default 404 page.

To develop a beta site, and add other folder for different purposes, I'd like to add these folders

  • example.com/beta
  • example.com/products/
  • example.com/photos

as exceptions in the HTACCESS file. Meaning, the site should read/render the content that is in these folders! Instead of redirecting OR producing content based on the main index (sample.com/index.php). Right now I can't access any files in example.com/beta/

Used all the answers found in StackOverflow: Exclude one folder however, all the answers had the unfortunate effect of breaking the whole site instead of just adding exceptions OR sends me to the default 404 page (the "exception" folders ALREADY have index files, 404 should not happen)

Adding any of the following answers, just below the first RewriteEngine on didn't work:

...
<IfModule mod_rewrite.c>
RewriteEngine   On
RewriteRule !^beta($|/) http://www.example.com%{REQUEST_URI} [L,R=301]
...
</IfModule>

OR

...
<IfModule mod_rewrite.c>
RewriteEngine   On
RewriteRule ^(beta) - [L]
...
</IfModule>

OR

...
<IfModule mod_rewrite.c>
RewriteEngine   On    RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
...
</IfModule>

At this point I am scratching my head as to what rule am I writing wrong to make the folder exceptions work. Below is the complete (original htaccess) file

Original HTACCESS

<IfModule mod_headers.c>
    Header unset ETag
    FileETag None
    <FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
        Header unset Last-Modified
        Header set Expires "Fri, 21 Dec 2020 00:00:00 GMT"
        Header set Cache-Control "public, no-transform"
    </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine   On
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^((.)?)$    index.php?p=home [L]

    RewriteCond $1 /var/www/vhosts/mywebsite.com/httpdocs
    RewriteRule ^(.+)$ / [L]

    RewriteCond $1 !^(\#(.)*|\?(.)*|index\.php(.)*|content\/(.)*|css\/(.)*|picture_library\/(.)*|img\/(.)*|readme\.txt(.)*|favicon\.ico(.)*|robots\.txt(.)*|download\.php(.)*|admin\.php(.)*|\.htaccess\.back(.)*|\.htaccess(.)*|external\-export\.php(.)*|login\.php(.)*|cgi-bin\/(.)*|ioncube\/(.)*|test\/(.)*|sitemap\.xml(.)*|old\/(.)*|index\.php\?p\=404(.)*|C079CC0215D2E77B89B0F1837C8199B1\.txt(.)*|images\/(.)*|index\.html(.)*)
    RewriteRule ^(.+)$ index.php?url=$1&%{QUERY_STRING} [L]
    RewriteEngine On     
        RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC] 
        RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301,L] 


    
</IfModule>

<IfModule mod_deflate.c>
    <FilesMatch "\.(js|css|ico|flv|jpg|jpeg|png|gif)$">
        SetOutputFilter DEFLATE
    </FilesMatch>
</IfModule>
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Omar
  • 11,783
  • 21
  • 84
  • 114

2 Answers2

1

You could have htaacess rules file in following manner. I could see many issues in your current htaccess file, let me mention them and their fixes one by one here.

<IfModule mod_headers.c>
    Header unset ETag
    FileETag None
    <FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
        Header unset Last-Modified
        Header set Expires "Fri, 21 Dec 2020 00:00:00 GMT"
        Header set Cache-Control "public, no-transform"
    </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On     
    RewriteCond %{HTTP_HOST} ^(?:www\.)?mywebsite\.com$ [NC] 
    RewriteRule ^(.*)/?$ http://www.mywebsite.com/$1 [R=301,L,NE]
    
    ##This Rules needs to be more clear. 
    ##RewriteCond %{QUERY_STRING} ^$
    ##RewriteRule ^(.*)/?$  index.php?p=home [QSA,L]

    ##This rule needs to be more clear too.
    RewriteRule ^httpdocs/?$ / [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1&%{QUERY_STRING} [L]

</IfModule>

<IfModule mod_deflate.c>
    <FilesMatch "\.(js|css|ico|flv|jpg|jpeg|png|gif)$">
        SetOutputFilter DEFLATE
    </FilesMatch>
</IfModule>

Issues fixed:

  • First thing first we need not to use RewriteEngine On many times, just on time is enough in htaccess.
  • Thumb rule never do domain redirection at bottom of rules, always place them at the top of htaccess file.
  • Added NE flag to redirection which is required for sure.
  • I observed you have used RewriteCond $1 in few rules, well that's not possible in this manner, you need to give which attribute you are looking for to be checked.
  • I have commented rule where it points to index.php if query string is NULL, well that is very much problematic since css/js/actual pages(uris) will come in this category and that will break your site.
  • You need to handle above by applying it only non-existing pages(when you do that your existing pages which you are not able to access now should be accessed well IMHO). I have commented RewriteCond %{QUERY_STRING} ^$ rule I see we need to use index.php rules 2 times 1 with ?=home QSA and 2nd one with normal one(index.php?url=$1&%{QUERY_STRING}) if you could tell me difference between when should 1st rule get apply and when 2nd then I can edit 1st one accordingly.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

Hi try this if it can solve your problem, here I am droping you my htaccess code if you found it useful please let me know.

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase RewriteRule ^.*$ http://www.http://www.example.com%{REQUEST_URI} [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>