Forum Moderators: phranque

Message Too Old, No Replies

Multiple RewriteCond in the same .htaccess

         

peshte

2:46 pm on Jan 24, 2011 (gmt 0)

10+ Year Member



Hy everyone.
I have a little problem for you:

Here is the original code that works on the web:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|.php|.html|.htm|.feed|.pdf|.raw|/[^.])$ [NC]
RewriteRule (.) index.php
RewriteRule . - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

This code redirects any request to index.php, except the real files and folders.


In the same .htaccess file I want to put a new RewriteCond/RewriteRule sequence but that apply for the localhost only.

The new code should look like this:

RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|.php|.html|.htm|.feed|.pdf|.raw|/[^.])$ [NC]
RewriteRule (.) index.php
RewriteRule . - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


RewriteCond %{HTTP_HOST} ?WHAT IS THE CONDITIONAL? [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|.php|.html|.htm|.feed|.pdf|.raw|/[^.])$ [NC]
RewriteRule (.) localhost_path/index.php
RewriteRule . - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

Please help.
Thank you.

g1smd

7:39 pm on Jan 24, 2011 (gmt 0)

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



Is this the code for Joomla per chance?

There's a thread around here with much more efficient code presented.

peshte

8:21 pm on Jan 24, 2011 (gmt 0)

10+ Year Member



This isn't for Joomla, but i haved inspired from a Joomla htaccess.

I've searched on Google a lot, but didn't find anything that works like I need.

I just need an "if" HTTP_HOST=localhost then Rewrite all requests to localhost_path/index.php , else Rewrite all requests to index.php or test/index.php . I need that for webdeveloping for not manually editing that htaccess again and again.

g1smd

8:40 pm on Jan 24, 2011 (gmt 0)

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



The code you have now is grossly inefficient.
There are many improvements that can be made.

[webmasterworld.com...]

jdMorgan

8:46 pm on Jan 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A much-more-efficient version of your original rule, which will do exactly the same thing but will likely result in a noticeable server speed increase, would be:

RewriteBase /
#
# Copy HTTP Authorization header value to HTTP_AUTOHRIZATION server variable for use by my script
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# Rewrite requests for URL-paths which do not resolve to specific physically-existing
# filetypes to my script, excluding the script's URL-path itself.
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^[^.]*|\.(php|html?|feed|pdf|raw))$ index.php [NC,L]

This is faster because it eliminates many unnecessary disk checks (-f and -d), eliminates one rewritecond by moving the pattern to the rewriterule itself, and uses more-optimized regular-expressions for that pattern.

I also moved the "copy auth header" rule to the top and modifed its pattern, so that it will always run -- even if you do add more rules.

To get your localhost development environment working, you'll need to modify the existing rule, and add a new one:

RewriteBase /
#
# Copy HTTP Authorization header value to HTTP_AUTOHRIZATION server variable for use by my script
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# On live server, rewrite requests for URL-paths which do not resolve to specific
# physically-existing filetypes to my script, excluding the script's URL-path itself.
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^[^.]*|\.(php|html?|feed|pdf|raw))$ index.php [NC,L]
#
# On localhost dev server, rewrite requests for URL-paths which do not resolve to specific
# physically-existing filetypes to my dev script, excluding the script's URL-path itself.
RewriteCond %{HTTP_HOST} ^localhost$
RewriteCond $1 !^localhost_path/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^[^.]*|\.(php|html?|feed|pdf|raw))$ localhost_path/index.php [NC,L]

Jim

peshte

4:56 am on Jan 25, 2011 (gmt 0)

10+ Year Member



Thank you for help.

I've made some tests and here is the final result:

RewriteBase /
#I don't use this type of authentification. It is safe to remove this line?
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

#For live use
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^[^.]*|\.(php|html?|feed|pdf|raw))$ test/index.php [NC,L]

#For local use
RewriteCond %{HTTP_HOST} ^localhost$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^[^.]*|\.(php|html?|feed|pdf|raw))$ index.php [NC,L]