trante
trante

Reputation: 34006

.htaccess Rewrite rules for stopping executing

Most of my htaccess rules redirects unsigned users to index.html.
But when I redirect them to index.html, all of these rules are re-run for that page also. I need to run a minimum set of rules for the users visiting index file.

It's obvious that [L] stops htaccess rules' running.

What would be the better way for this issue? Is this a proper way to do it or do you have any better recommendations?

RewriteCond %{REQUEST_URI} index\.html
RewriteRule ^(.*)$ index.html [L]

My full htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    RewriteCond %{REQUEST_URI} (\/onefolder|\/secondfolder) [NC]
    RewriteCond %{REMOTE_ADDR} !111\.22\.33\.44 [NC]
    RewriteCond %{REQUEST_URI} !(index\.html) [NC]
    RewriteRule ^(.*)$ index.html [F]

    RewriteCond %{HTTP_USER_AGENT} oneagent|otheragent [NC]
    RewriteCond %{REQUEST_URI} !(index\.html) [NC]
    RewriteRule ^(.*)$ index.html? [R=301,L]

    RewriteCond %{HTTP_USER_AGENT} betteragent|otherbetterganet [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(index\.html) [NC]
    RewriteRule ^(.*)$ index.html? [R=301,L]

    RewriteCond %{HTTP_COOKIE} !username [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(\/importantone|\/somefolder|index\.html) [NC]
    RewriteRule ^(.*)$ index.html [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Upvotes: 2

Views: 2062

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

A few things you can do. If you want to simply stop rewriting for index.html, you can do something similar to what you have (but simpler), at the very top of your rules:

RewriteRule ^/?index.html$ - [L]

The - is simply to "pass through", essentially making it so rewriting stops there.

Alternatively, you can prevent looping completely by adding this to the top of your rules:

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

Does something similar but checks if there was an internal redirect of any kind.

Upvotes: 2

Related Questions