Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite suggestions

         

milanmk

12:02 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



Am I using mod_rewrite in an efficient manner in the following conditions?

Condition 1 : Redirect all requests except homepage to other domain.

<VirtualHost>

RewriteEngine on
RewriteCond %{REQUEST_URI}!^/$
RewriteCond %{REQUEST_URI}!^/index\.html$
RewriteRule .* http://example.com/ [R=301,L]

</VirtualHost>

Condition 2 : Redirect all sub-domain favicon requests to main domain, where all sub-domains are dynamic domains.

<VirtualHost>

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/favicon\.ico$
RewriteRule .* /home/example/public_html/favicon.ico [L]

</VirtualHost>

Please post your suggestions for improvement.

Milan

jdMorgan

4:13 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In both cases there is no need to use a separate RewriteCond to test REQUEST_URI, since RewriteRule can do this directly. In the first rule, no "OR" is needed; Simply make the "index\.html" optional so that the requested URL-path can be either "/" or "/index.html" but nothing else will match.

Readers please note that this code is written for use in httpd.conf or conf.d, not in .htaccess.


<VirtualHost>
RewriteEngine on
# Externally [b]redirect[/b] all except home page requests to other domain
RewriteRule !^/(index\.html)?$ http://example.com/ [R=301,L]
</VirtualHost>
#
<VirtualHost>
RewriteEngine on
# Internally [b]rewrite[/b] subdomain-subdirectory favicon requests to common/shared directory favicon
RewriteRule ^/([^/]+/)+favicon\.ico$ /home/example/public_html/favicon.ico [L]
</VirtualHost>

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

milanmk

5:44 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



Got it Jim.

You recommended RewriteLog in some other posts, so I configured it and it was the best debugging I ever need for mod_rewrite.

I thought if we could just trigger rewrite engine when needed by using <Files>.

<VirtualHost>

<Files "favicon.ico">
RewriteEngine on
RewriteRule ^/([^/]+/)+favicon\.ico$ /home/example/public_html/favicon.ico [L]
</Files>

</VirtualHost>

But it didn't worked that way and redirected to the following URI

/home/example/public_html/web/home/example/public_html/favicon.ico

while RewriteLog showed correct behavior

[per-dir favicon.ico/] applying pattern '^/([^/]+/)+favicon\.ico$' to uri '/home/example/public_html/web/favicon.ico'
[per-dir favicon.ico/] rewrite /home/example/public_html/web/favicon.ico -> /home/example/public_html/favicon.ico
[per-dir favicon.ico/] internal redirect with /home/example/public_html/favicon.ico [INTERNAL REDIRECT]

Any clues?

Milan

jdMorgan

6:25 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like DocumentRoot isn't set correctly. Also, see the mod_rewrite RewriteBase directive, and finally, look for any other directives that can affect the URL-path or filepath, such as the directives in mod_alias.

Jim

milanmk

1:44 pm on Mar 10, 2007 (gmt 0)

10+ Year Member



Checked all the directives but did not find anything wrong. Anyway, I am certainly happy with your suggestion given in msg#2.

Milan

jdMorgan

2:46 pm on Mar 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In this rule

RewriteRule ^/([^/]+/)+favicon\.ico$ /home/example/public_html/favicon.ico [L]

be aware that the "/home/example/public_html part" of the path is a local URL-path. The value of DocumentRoot will be prepended to this in order to resolve the URL-path to a local filepath. Therefore, it may be that you should not use /home or /home/example/ or even /home/public_html in your rule at all. That is, something like

RewriteRule ^/([^/]+/)+favicon\.ico$ /public_html/favicon.ico [L]

might work better, so you may wish to experiment with this.

Jim

milanmk

5:41 pm on Mar 10, 2007 (gmt 0)

10+ Year Member



I tried all the possible combination of RewriteRule as suggested by you but every time "local URL-path" (/home/example/public_html) was prefixed automatically.

Then I suddenly found this in the RewriteLog

... [per-dir favicon.ico/] …

So, I guess the <Files> directive is supposedly meant for per directory manipulation, adding local URL-path by default.

Milan

jdMorgan

6:24 pm on Mar 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it's more likely that it means that favicons are being rewritten in an .htaccess file (per-dir configuration file) somewhere in the path.

The important part of the idea I posted was this: URLs and filepaths are different things, and need not in fact have any relationship to each other; If you use mod_rewrite, you can map /red to /blue, or /big to /small, and no-one the wiser. In order to form a local server filepath from a requested URL, the server does this

Remove the domains name
Apply mod_alias directves
Apply mod_rewrite directives
Add the value of DocumentRoot to the resulting local URL-path (convert it to a local server filepath)
Read the resulting file (or run it as a script) and send the contents to the requesting client

So the main point is that you should not be adding the full server path information to the substitution URL; Add only the part that comes after the DocumentRoot value.

You may also wish to look into the RewriteBase directive in mod_rewrite if you hit a brick wall.

The good news is that once you figure out the correct path information for your particular server's set-up, it will always be the same for all RewriteRules.

Jim

milanmk

6:03 am on Mar 11, 2007 (gmt 0)

10+ Year Member



I think I should show you my exact http.conf configuration. I have one main domain and one sub-domain which points to sub-directory within main domain.

<Directory /home/example/public_html>
Options -Indexes
AddType application/x-httpd-php .php .htm
</Directory>

<Directory /home/example/public_html/logs>
Deny from All
</Directory>

<VirtualHost>
ServerName example.in
ServerAlias www.example.in
ServerAdmin webmaster@example.in
DocumentRoot /home/example/public_html
BytesLog domlogs/example.in-bytes_log
CustomLog /usr/local/apache/domlogs/example.in combined
ScriptAlias /cgi-bin/ /home/example/public_html/cgi-bin/

User example
Group example

RedirectMatch 301 /support-(.*) http://example.in/su-$1

RewriteEngine on
RewriteCond %{HTTP_HOST}!^example\.in [NC]
RewriteRule ^/(.*) http://example.in/$1 [R=301,L]
</VirtualHost>

<VirtualHost>
ServerName web.example.in
ServerAlias *.example.in
ServerAdmin webmaster@web.example.in
DocumentRoot /home/example/public_html/web
BytesLog domlogs/web.example.in-bytes_log
CustomLog /usr/local/apache/domlogs/web.example.in combined
ScriptAlias /cgi-bin/ /home/example/public_html/web/cgi-bin/

User example
Group example

<Files "favicon.ico">
RewriteEngine on
RewriteRule ^/([^/]+/)+favicon\.ico$ /public_html/favicon.ico [L]
</Files>

</VirtualHost>

Server processing for h**p://web.example.in/favicon.ico

Apply mod_alias directives : none

Apply mod_rewrite directives :
applying '^/([^/]+/)+favicon\.ico$' to /home/example/public_html/web/favicon.ico -> matched
rewrite /home/example/public_html/web/favicon.ico -> /public_html/favicon.ico

Add the value of DocumentRoot to the resulting local URL-path :
/public_html/favicon.ico -> /home/example/public_html/web/public_html/favicon.ico

This means apache will not let traverse above my DocumentRoot within VirtualHost, or am I still misunderstanding it?

Milan

jdMorgan

4:01 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> This means apache will not let traverse above my DocumentRoot

Yes, that is correct. To do otherwise would be to invite an impossible security-management problem.

If you want to share images across multiple Vhosts, then define an alias "shared" subdirectory in each vhost as a *nix Symbolic Link. Such a link looks like a subdirectory to the user inside that vhost, but is actually a pointer to a directory outside his vhost.

You could also try defining the favicon directory as a server-wide Alias directory -- See Apache mod_alias.

In both cases. make sure you are the only user with write permission on the real directory.

Jim

milanmk

7:48 pm on Mar 11, 2007 (gmt 0)

10+ Year Member



Right, server wide Alias will be a good option. Back to testing...

Thank you Jim, once again.

Milan