mod_rewrite infite loop - apache

i have Apache 2.2 and 3 virtual hosts on it and a mod_rewrite infinite lopp isssue i cannot resolve. Apache is in my company LAN on the 192.168.1.228 IP address. 2 virtual hosts work perfectly. The third makes problems. My network admin created a subdomain to point to that 192.168.1.228. The Virtual Host is configured this way:
<VirtualHost 192.168.1.228>
DocumentRoot "C:/Apache/htdocs/stansrodowiska"
ServerName giostube.mycompany.local
LimitInternalRecursion 1000
<Directory "C:/Apache/htdocs/stansrodowiska">
Options Indexes FollowSymLinks Includes
AllowOverride None
Order allow,deny
Allow from all
RewriteEngine on
RewriteCond $1 !^(index\.php|images|upload|css|download|assets|js|nfos|robots\.txt)
RewriteRule ^(.*)$ /stansrodowiska/index.php/$1 [L]
</Directory>
</VirtualHost>
If i access this website from my lan over the address: http://giostube.mycompany.local/ i get an
403 Forbidden
You don't have permission to access /stansrodowiska/index.php/stansrodowiska/index.php/stansrodowiska/index.php/stansrodowiska/index.php/stansrodowiska/index.php/stansrodowiska/index.php/stansrodowiska/index.php......... on this server
The /stansrodowiska/index.php/ is of course repeated much more often i think to around 2048 chars which is the HTTP POST maximum.
Strange thing is that exactly the same mod_rewrite rules work on an other preconfigured WAMP environment (Vertrigo).
Underneath there is a Codeigniter 1.7.2 application.
Is the error within the mod_rewrite rules or do i have to search elsewhere?

Change this line:
RewriteRule ^(.*)$ /stansrodowiska/index.php/$1 [L]
To:
RewriteRule ^(.*)$ /index.php/$1 [L]

Related

Made exact same htaccess change to allow one IP address, but it's not working

I just changed web hosts and I'm trying to re-apply some of the same IP address restrictions on my webhost. However, making the exact same changes does not appear to be working.
For example, I'm wanting to allow only one IP address, here's what was on my old web host:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
order deny,allow
deny from all
allow from xx.xx.xx.xx
</IfModule>
with my IP address obfuscated obviously. However, when I do that on the new web server, it seems to not even allow my IP address. I'm not 100% exactly sure why this is happening if this is the exact same configuration.
For what it is worth, I'm using VirtualHosts, so I have configurations for the virtual hosts in /etc/apache2/sites-available/domain.conf file, but nothing for mod_rewrite or anything pertaining to IP address restrictions.This change is being made in the .htaccess file in the HTML directory for the specific web host. This change is being made in the .htaccess file in the HTML directory for the specific web host.
If I simply remove those 3 lines, everything works perfectly fine, so I'm a little puzzled.
Updated
So I tried to make adjustments by using "Require", as preferred in Apache 2.4, and it seems like I'm still getting access denied. Here's an example of my virtual host configuration file:
DocumentRoot /var/www/html/mysite
<Directory /var/www/html/mysite/>
Options Indexes FollowSymLinks
AllowOverride All
#Require all granted
<IfModule mod_authz_core.c>
<RequireAny>
Require ip <myip>
</RequireAny>
</IfModule>
</Directory>
I've tried with IfModule and without it, and the same goes for RequireAny. I tried replacing "Require all granted" with that one Require ip <myip> statement and still getting denied.

Apache port being automatically forwarded

I'm trying to have two different directory associated to two different ports. (8001 and 8004), but whenever I try to access port 8004 in a browser, it redirects automatically to port 8001.
Here's some extract of the config files that seems relevant:
In httpd.conf:
Listen 80
Listen 8001
Listen 8004
Removed comment to line:
Include etc/apache24/extra/httpd-vhosts.conf
In file httpd-vhosts.conf:
<VirtualHost *:8001>
DocumentRoot "<path_to_site_1>"
<Directory "<path_to_site_1>">
Require all granted
</Directory>
<Directory "<path_to_site_1>">
AllowOverride All
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</Directory>
</VirtualHost>
<VirtualHost *:8004>
DocumentRoot "path_to_site_2"
<Directory "path_to_site_2">
Require all granted
</Directory>
<Directory "path_to_site_2">
AllowOverride All
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</Directory>
</VirtualHost>
Here's a list of things I've tried, that didn't change anything:
Having the VirtualHost declaration in httpd.conf
Changing the order of the Virtual host declaration
Changing port 8004 to another port
Clear the browser cache and try a different browser
If I comment Listen 8001, it breaks everything.
Port 80 seems to point to the default www directory of this FreeBSD installation and still works properly, for phpMyAdmin is installed there and is still accessible.
Maybe of note, it's a FreeBSD installation running on Oracle VM VirtualBox.
Thank you for any help or ideas to follow.
Unfortunately, in this case, the comportment wasn't due to Apache configuration but to the website installations. One was a WordPress installation and the other was a Drupal installation. I wasn't aware that by moving those website in the directory structure, they would no longer point at the right place. WordPress was doing the redirection.
The issue was solved with two different solution, one for each platform.
For WordPress:
In the file functions.php, located in the theme directory:
Add the following lines:
update_option( 'siteurl', '<new_url>' );
update_option( 'home', '<new_url>' );
In the browser, fefresh the website a few time
Remove both lines from the file
For Drupal:
(using drush) In the directory where the website is located:
drush cache-rebuild
The combination of both commands solved the issue with both installation.

"RewriteEngine not allowed here" caused by htaccess in parent directory

Currently I am using OSX Server (Yosemite) to host a bunch of PHP applications, some of which have a sub-directory under the websites document root for subdomains. Since updating to the Yosemite version of OSX Server, these subdomains have been throwing a 500 error with the error log referring to RewriteEngine not allowed here.
Investigating, I have confirmed that both the parent and subdomain sites have AllowOverride All configured, and .htaccess files are working on non-subdomain sites. Also, I have discovered that renaming or otherwise removing the .htaccess file from the parent directory causes the sub-domains to start working again.
/original_site_doc_root <- doc root for regular site
.htaccess
index.php
...
subdomain/ <- configured as a seperate site in osx server as a subdomain
.htaccess
index.php
...
Every bit of googling I do ends up referring to making sure mod_rewrite is installed and AllowOverride is configured properly.
My question is, how can I get Apache to stop throwing a 500 error on the sub-domain sites?
Edit
Here is the .htaccess file for the sub-domain that is causing me grief (with domains, directories and pages fuzzed to protect the innocent)
RewriteEngine On
Options +FollowSymlinks -Indexes
RewriteCond %{HTTP_HOST} ^www\.m\.somesite\.com$ [NC]
RewriteRule ^(.*)$ http://m.somesite.com/$1 [L,R=301]
RewriteBase /
# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
# enable PHP error logging
php_flag log_errors on
php_value error_log /some/fuzzed/dir
RewriteRule ^$ /index.php [L]
RewriteRule ^home$ /home.php [L]
RewriteRule ^some-page1$ /some-page1.php [L]
RewriteRule ^some-page2$ /some-page2.php [L]
RewriteRule ^some-page3/(.*)$ /some-page32.php [L]
RewriteRule ^some-page3(\/?)$ /some-page32.php [L]
RewriteRule ^some-page4/(.*)$ /some-page4.php [L]
RewriteRule ^some-page4(\/?)$ /some-page4.php [L]
RewriteRule ^some-page5/(.*)$ /some-page5.php [L]
RewriteRule ^some-page5(\/?)$ /some-page5.php [L]
RewriteRule ^some-page6/(.*)$ /some-page6.php [L]
RewriteRule ^some-page6(\/?)$ /some-page6.php [L]
The .htaccess for the parent directory/non-sub-domain-site is more or less similar, with the only real difference of relevance being the top 2 lines:
RewriteEngine On
Options +FollowSymlinks -Indexes -Multiviews
Just ran into this same problem (after recent update Apache2) and found a solution.
Assume your domain is example.com and the directory is /var/www/example/, and you had a subdomain called api.example.com with directory /var/www/example/api/.
Try to use the following:
<VirtualHost *:80>
ServerName api.example.com
DocumentRoot /var/www/example/api
<Directory /var/www/example/>
AllowOverride FileInfo Options
</Directory>
<Directory /var/www/example/api/>
Require all granted
Options +FollowSymLinks +MultiViews +ExecCGI -Indexes
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This should work!
Since updating to the Yosemite version of OSX Server, these subdomains have been throwing a 500 error with the error log referring to RewriteEngine not allowed here.
OS X 10.10 (Yosemite) ships with Apache 2.4, whereas earlier versions of OS X ship with Apache 2.2. An important difference between these versions, and which is probably causing you problems, is that AllowOverride defaults to All in Apache 2.2 and None in Apache 2.4. (See the Apache Docs) You are perhaps relying on this default behaviour.
I have confirmed that both the parent and subdomain sites have AllowOverride All configured
But where exactly are the AllowOverride All directives set? I assume the parent and subdomain sites are configured as separate Virtual Hosts. If AllowOverride All is only set in the parent domain's VirtualHost, for the "parent directory" (of the subdomain), then this will not been seen when you access the subdomain, which is an entirely separate VirtualHost. You will need to redeclare this in the subdomain's own (isolated) VirtualHost as well (as in #benck's answer).
Or, if you don't want the parent .htaccess file to be processed at all then explicitly set the following in the subdomain's VirtualHost:
<Directory /var/www/example/>
AllowOverride None
AllowOverrideList None
</Directory>
.htaccess files work along the filesystem path, regardless of the host being accessed.
Alternatively, if the main domain is configured in the main server config (ie. not in a VirtualHost container) then you shouldn't have this problem, as the VirtualHost will inherit the server's configuration.
Reference:
https://en.wikipedia.org/wiki/MacOS_Server#OS_X_10.10_(Yosemite_Server_4.0)

Why is Apache Permanent Redirect removing the slash between the domain and the path?

I'm using Apache 2.4, and I set up two virtual directories. One requires SSL, and the other one redirects to it.
If a user attempts to visit https://www.derp.com/derp without /derp existing, they correctly get a 404. But when a user visits http://www.derp.com/derp, Apache incorrectly redirects the user to https://www.derp.comderp, removing the slash between the path and the domain name.
I have no idea what would be causing this.
The following is the setup of my Virtual Host.
<VirtualHost *:443>
ServerAdmin derp#derp.com
ServerName www.derp.com
ServerAlias derp.com
DocumentRoot "C:\Users\derp\Documents\Web Projects\derp"
SSLEngine on
SSLCertificateFile "C:\Apache24\certs\cert.cer"
SSLCertificateKeyFile "C:\Apache24\certs\key.key"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin derp#derp.com
ServerName www.derp.com
ServerAlias derp.com
Redirect permanent / https://www.derp.com/
</VirtualHost>
<Directory "C:\Users\derp\Documents\Web Projects\derp">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
SSLRequireSSL
</Directory>
Why would Apache be behaving this way?
Bonus Question: Should redirects be handled in my virtual host definition, or should it be handled in the .htaccess file in the web site's physical directory?
Edit:
I'm starting a Laravel project, and by default the public folder does contain a .htaccess file, so here's that guy:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Edit Two:
I tried:
adding a slash at the end of the DirectoryRoot path
replacing the backslashes with forward slashes in the DirectoryRoot path
replacing the backslashes with double backslashes in the DirectoryRoot path
I also removed the .htaccess file from the directory completely.
It redirects correctly when you go from http://www.derp.com to https://www.derp.com. It's just when you specify a path and attempt https that it removes the slash between the domain and the path.
Edit Three:
I also attempted the following suggestion:
Redirect permanent / https://www.derp.com/
Try
RedirectMatch permanent /(.*) https://www.derp.com/$1
or
RedirectMatch permanent (.*) https://www.derp.com/$1
... and instead of redirecting to https://www.derp.comderp, it instead does not redirect, attempts and gives a 404 for http://www.derp.com/derp, but using Apache's 404, instead of throwing a Not Found Exception, as Laravel does without configuration.
Edit Four:
I have also tried:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
In the .htaccess file and the behavior did not change at all.
I got it.
The issue did not lay with the rewriting at all, it was the SSLRequireSSL directive under my Directory definition that was causing the problem.
I simply removed this directive, refreshed the cache in all of my browsers, and the site then continued to work correctly. This was discovered through the process of elimination.
The documentation notes:
This directive forbids access unless HTTP over SSL (i.e. HTTPS) is enabled for the current connection. This is very handy inside the SSL-enabled virtual host or directories for defending against configuration errors that expose stuff that should be protected. When this directive is present all requests are denied which are not using SSL.
The emphasis is my own. SSLRequireSSL may have Apache only return a 403 or 404 if HTTP over SSL is not enabled, interfering with the Redirect rule. A rewrite rule such as the one in this answer on Server Fault may be a better alternative depending on your use case:
RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]
My issue was related to browser caching.
I tried it in a different browser and it worked and then tried again in a private session in the first browser and it also worked.

I'm confused with Apache vhost

I am building a web application with Zend Framework, and I need to point my app to the "public" folder of the application:
So basically when I call http://localhost/myapp
it should display http://localhost/myapp/public/
I created a virtual host file called myapp into /etc/apache2/sites-available/:
<VirtualHost *:80>
DocumentRoot /var/www/myapp/public/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/myapp/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
But it doesn't work. When I call http://localhost/myapp, it displays the directory structure of the app, and when I click on the "public" folder, then it displays what I want to be displayed by default...
I never configured vhosts before and that's as far as I got with the tutorials about it.
In your first listing, you had a different value for the Directory tag, leaving off 'public' altogether. There was also a trailing slash after 'public' in the DocRoot value, but removed on your second attempt. Not sure that made a difference, but I believe it's recommended that you don't include trailing slashes.
Also, just wondering...are you running this on a local machine? I had trouble with Skype wanting to use port 80 if I started running that before my apache server. Skype will use a different port if 80 is already used. If not Skype, there may be another app that's using port 80 and interfering. That could be why you had success on another port.
Ok I found a way somehow... I don't think it's necessary the right/best way but...
in httpd.conf (in apache2 folder):
Listen 10089
<VirtualHost *:10089>
DocumentRoot "/var/www/myapp/public"
<Directory "/var/www/myapp/public">
Order allow,deny
Allow from all
AllowOverride all
</Directory>
</VirtualHost>
My app is now accessible via localhost:10089
After enabling the rewrite mod in apache, I added the necessary .htaccess, one at the root of my app, redirecting everything to index.php (Zend framework support friendly url navigation and works that way):
RewriteEngine on
RewriteRule .* index.php
and a second .htaccess file inside my public folder to allow people to access .jpg,.ico,etc files and not being redirected to index for everything:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Hope this will help some!