Forum Moderators: phranque

Message Too Old, No Replies

Apache rewrite?

         

Khairun

1:02 am on Oct 17, 2011 (gmt 0)

10+ Year Member



Hello, my first post here.. please I desperately need help to torubleshoot...
I'm trying to run my own web application that requires
all request made to mywebapps, are request made to the "index.php"
file. so mywebapps trusts the apache rewriting system to transform any "/<mywebapps>"
to "/index.php/<mywebapps>".

Here's my apache directives configuration
SuexecUserGroup "#1006" "#1007"
ServerName myapps.domain.com
ServerAlias www.myapps.domain.com
ServerAlias webmail.myapps.domain.com
ServerAlias admin.myapps.domain.com
DocumentRoot /home/domain.com/domains/myapps.domain.com/public_html
ErrorLog /var/log/virtualmin/myapps.domain.com_error_log
CustomLog /var/log/virtualmin/myapps.domain.com_access_log combined
ScriptAlias /cgi-bin/ /home/domain.com/domains/myapps.domain.com/cgi-bin/
DirectoryIndex index.html index.htm index.php index.php4 index.php5
<Directory /home/domain.com/domains/myapps.domain.com/public_html>
Options -Indexes +IncludesNOEXEC +FollowSymLinks
allow from all
AllowOverride All
Action application/x-httpd-php5 /cgi-bin/php5.cgi
AddType application/x-httpd-php5 .php5
AddType application/x-httpd-php5 .php
</Directory>
<Directory /home/domain.com/domains/myapps.domain.com/cgi-bin>
allow from all
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.myapps.domain.com
RewriteRule ^(.*) http://myapps.domain.com:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.myapps.domain.com
RewriteRule ^(.*) https://myapps.domain.com:10000/ [R]
RemoveHandler .php
RemoveHandler .php5


While mywebapps have its own configuration on it .htaccess

Options +FollowSymLinks +ExecCGI

AddDefaultCharset utf-8

<IfModule mod_authz_host.c>
<FilesMatch "\.(lock|conf)$">
Order deny,allow
Deny from all
</FilesMatch>

<FilesMatch "pre_.+\.php">
Order deny,allow
Deny from all
</FilesMatch>
</IfModule>


<IfModule mod_rewrite.c>
RewriteEngine On


But those rewrite doesnt works, as I still need to force retype link to
myapps.domain.com/index.php/mywebapps

while several link inside mywebapps showing that the link to
http://myapps.domain.com/cgi-bin/php5.cgi/mywebapps


And here's the error log on the apache

[Mon Oct 17 08:52:45 2011] [error] [client 110.139.177.53] Premature end of script headers: php5.cgi, referer: http://myapps.domain.com/mywebapps


My goal is..

all request made to mywebapps, are request made to the "index.php"
file. so mywebapps trusts the apache rewriting system to transform any "/<mywebapps>"
to "/index.php/<mywebapps>".




Any suggestion, I really appreciated...

lucy24

3:24 am on Oct 17, 2011 (gmt 0)

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



<IfModule mod_rewrite.c>
RewriteEngine On


But those rewrite doesnt works

But what rewrite? You stopped just when it was getting interesting!

The only Rewrites in sight are in the config file-- which, incidentally, would benefit from a few blank lines. Or, preferably, a lot of blank lines, so everyone can see what's what.

RewriteEngine on
RewriteCond %{HTTP_HOST} =webmail.myapps.domain.com
RewriteRule ^(.*) http://myapps.domain.com:20000/ [R]
RewriteCond %{HTTP_HOST} =admin.myapps.domain.com
RewriteRule ^(.*) https://myapps.domain.com:10000/ [R]


You don't need the beginning anchor ^ since you are not naming some specific text that has to come at the beginning. And you don't need the parentheses, because you are not capturing anything for later use. So just .* will do.

End all Rewrites with the [L] flag unless you have a clear and specific reason for leaving it out.

[R] is redundant, because the protocol-plus-path has already made the Rewrite into a Redirect. What you do need is [R=301]. Redirects are permanent unless, again, there's a clear and specific reason for making them temporary (unfortunately 302 is the default).

In general, you don't need the <If...> parts. You either have a module or you don't; it doesn't drift in and out from day to day.

------

And now let's see what that config file unpacks to if you've got it spread out so you can see it.

SuexecUserGroup "#1006" "#1007"
ServerName myapps.domain.com
ServerAlias www.myapps.domain.com
ServerAlias webmail.myapps.domain.com
ServerAlias admin.myapps.domain.com

DocumentRoot /home/domain.com/domains/myapps.domain.com/public_html
ErrorLog /var/log/virtualmin/myapps.domain.com_error_log
CustomLog /var/log/virtualmin/myapps.domain.com_access_log combined

ScriptAlias /cgi-bin/ /home/domain.com/domains/myapps.domain.com/cgi-bin/

DirectoryIndex index.html index.htm index.php index.php4 index.php5

<Directory /home/domain.com/domains/myapps.domain.com/public_html>
Options -Indexes +IncludesNOEXEC +FollowSymLinks
allow from all
AllowOverride All
Action application/x-httpd-php5 /cgi-bin/php5.cgi
AddType application/x-httpd-php5 .php5
AddType application/x-httpd-php5 .php
</Directory>

<Directory /home/domain.com/domains/myapps.domain.com/cgi-bin>
allow from all
</Directory>

RewriteEngine on

RewriteCond %{HTTP_HOST} =webmail.myapps.domain.com
RewriteRule ^(.*) http://myapps.domain.com:20000/ [R]

RewriteCond %{HTTP_HOST} =admin.myapps.domain.com
RewriteRule ^(.*) https://myapps.domain.com:10000/ [R]

RemoveHandler .php
RemoveHandler .php5


DirectoryIndex index.html index.htm index.php index.php4 index.php5

This seems excessive. These are your own files on your own server, right? So you know what the filenames are going to be: either .htm or .html; either .php or .php{number}. Don't make the server waste time looking for filenames that it will never find. And I really don't think you want to be mixing .php4 and .php5.

g1smd

7:11 am on Oct 17, 2011 (gmt 0)

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



@Khairun You mentioned "rewrites" but the only RewriteRule code in sight is for a series of "redirects".

By using the R flag, you are telling the browser to make a new request for a new URL, a 302 redirect. The address bar WILL change to show that new URL.

Did you perhaps intend to use the [P] or [PT] flag instead?

Khairun

8:29 am on Oct 17, 2011 (gmt 0)

10+ Year Member



Sorry I missed the full .htaccess
By the way here's the full...

Options +FollowSymLinks +ExecCGI

AddDefaultCharset utf-8

<IfModule mod_authz_host.c>
<FilesMatch "\.(lock|conf)$">
Order deny,allow
Deny from all
</FilesMatch>

<FilesMatch "pre_.+\.php">
Order deny,allow
Deny from all
</FilesMatch>
</IfModule>


<IfModule mod_rewrite.c>
RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
RewriteBase /

# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Khairun

8:45 am on Oct 17, 2011 (gmt 0)

10+ Year Member



@Lucy and
@g1smd

Thank you for your reply ... about the apache's directives configuration on my virtual server (virtualmin).
It was automatically generated by the Virtualmin, in this part I only accept generic setting from Virtualmin as I newbie with this function.
Those two configuration (apache directive and .htaccess) was automatically generated by the system. So I haven't clue about their function line by line, sorry I will try elaborate more on your question, I can see somehow that you guys trying to minimize the problem. But I don't see how to achieves my goal here. Meanwhile I will try to digest more in this issues and your suggestion. Thank you very much and I really appreciate it. If you have any suggestion so I can trouble shoot more please shoot. I will try to give you my result and feed back asap.

Khairun

9:38 am on Oct 17, 2011 (gmt 0)

10+ Year Member



Hi,

Just find the solution... just need change mode to mod_php. by doing this ..

apt-get install libapache2-mod-php5 --reinstall
Following this thread .. [virtualmin.com...]
I'm not sure what was the problem, I think i missed the php5 module.

Thankls for all of your help..
So far so good thanks...

g1smd

6:31 pm on Oct 17, 2011 (gmt 0)

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



You should add the [L] flag to every RewriteRule.

To make it easier to read the code, add a blank line after every RewriteRule.

Make sure there are no blank lines after any of the RewriteCond lines.

lucy24

3:41 am on Oct 18, 2011 (gmt 0)

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



Make sure there are no blank lines after any of the RewriteCond lines.

! :o ! Does mod_rewrite do something dreadful, like ignore your Conditions if there's an extra blank line?

Oh, goody. An htaccess blunder I have yet to make.

Blank lines and white space occurring before a directive are ignored, so you may indent directives for clarity.

That's for config files. The htaccess tutorial is silent. If Apache has a section on general syntax, it is well concealed.

Or did you mean that if there are blank lines between any Conditions, the ones before the blank line(s) will be ignored?

Khairun

4:32 am on Oct 18, 2011 (gmt 0)

10+ Year Member



You should add the [L] flag to every RewriteRule.


Will do this.. thanks again..

g1smd

6:42 am on Oct 18, 2011 (gmt 0)

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



If you look at the code above there is

# some comment
RewriteRule ...
RewriteRule ...
RewriteCond ...

# some comment
RewriteRule ...

That layout makes the code difficult to read. The fact that the last rule has a condition is easily lost.

So, blank line after RewriteRule, no blank lines after RewriteCond clears this up.

lucy24

8:01 pm on Oct 18, 2011 (gmt 0)

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



:: mopping brow ::

OK, so that's "should" for your own sake, not "should" for the sake of avoiding server meltdown. With Apache, it is good to get these things spelled out ;)