Apache Rewrite rules not working as expected - apache

I have two apache servers both have identical settings, I cloned the apache config files, and changed the ServerName part only. When I type mysite.com/somestuff it should rewrite to index.php it does it on my old server, but not my new. I have made sure the .htaccess is there when I do mysite.com/index.php/somestuff it works, but like my first site I need it to work with mysite.com/somestuff.
I am really banging my head against the wall here is my .htaccess and apache config file
#.htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}index.php/ [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /index.php/
</IfModule>
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
Now for my apache config
<VirtualHost *:80>
ServerAdmin user#host.com
ServerName mysite.com
DocumentRoot /home/richardw/www/halogen/web
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /home/richardw/www/halogen/web/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
My apologizes if this is a repost, but I've been searching all over and i'm about to lose it.

When you go to http://mysite.com/, are you redirected to http://mysite.com/index.php/?
If this is happening, that means mod_rewrite is not loaded in your new server. You need to make sure it's loaded in your apache's server config file. See this answer for some instructions on how that works for apache.
The reason why the redirect is working is because of this container:
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /index.php/
</IfModule>
</IfModule>
This essentially says "if mod_rewrite is not loaded", then if mod_alias is loaded, it redirects the root request to /index.php/. So if the redirect is happening, mod_rewrite is not loaded.

Have you try to comment or remove this line
Options +FollowSymlinks
in your .htaccess?

Related

http://example.com would not redirect to https://www.example.com

we have this two conf on our webserver.
http:
<VirtualHost *:80>
ServerName www.example.de
ServerAlias example.de www.example.at example.at www.example.net example.net
RewriteEngine On
RewriteCond %{HTTP_HOST} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}
# Error page is just the index telling about the situation of not being connected
ErrorDocument 404 /index.html
</VirtualHost>
https:
<IfModule mod_ssl.c>
NameVirtualHost *:443
<VirtualHost *:443>
ServerName www.example.de
ServerAlias example.de www.example.at example.at www.example.net example.net
SSLEngine On
SSLCertificateFile /etc/letsencrypt/live/www.example.de/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.de/privkey.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}$1 [R=301,L]
DocumentRoot /var/www/homepage/web
<Directory /var/www/homepage/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/example-ssl.error.log
CustomLog ${APACHE_LOG_DIR}/example-ssl.access.log combined
</VirtualHost>
</IfModule>
When I try to open http://www.example.com then it would be redirect to https://www.example.com
When I try to open http://example.com or https://example.com, then I got the error "Page not available"
What is wrong in my both conf-Files on the Webserver?
Both should redirected to the https://www.example.com url.
Please, try to avoid mod_rewrite unless there is no other option. As you can see it's complicated and will make your life harder for the simpler tasks.
A single Redirect will do to redirect everything to the SSL virtualhost:
Redirect / https://www.example.de/
Note: Redirect depends on mod_alias, so no need to RewriteEngine on or such either in the non-SSL virtualhost.
If you insist on using mod_rewrite because you have many names and need the variables:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [R,L]
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L]
The first one catches those cases where host does not start with www, the second catches the rest.

Apache: difference virtualhost and htaccess

I set up a private server (raspberry) with apache.
the config file for the website:
<VirtualHost *:80>
ServerName me.example.com
ServerAlias me.example.com
RewriteEngine On
#only rewrite if not one of the followed files is requested
RewriteCond %{REQUEST_URI} !^/images/.*\.(jpg|ico|png|mp4|ogg)
RewriteCond %{REQUEST_URI} !^/css/.*\.css
RewriteCond %{REQUEST_URI} !^/js/.*\.js
#rewrite all requests to index.php and get path (only intern)
RewriteRule ^/([^/]*)/(.*)$ /index.php?lang=$1&path=$2 [NC]
DocumentRoot /var/www/my/root
<Directory /var/www/my/root>
Options FollowSymLinks
Options -Indexes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
1.) If I request now a "subpage" like /impressum.php. It will show me the impressum, instead of rewriting to the index file. Why?
2.) Now I copied the part of the rewrite engine into a .htaccess file and it doesn't work anymore. Where is the difference between these two files? What do I need to change, so it will work?
My .htaccess file looks like:
Options +FollowSymLinks
RewriteEngine On
#only rewrite if not one of the followed files is requested
RewriteCond %{REQUEST_URI} !^/images/.*\.(jpg|ico|png|mp4|ogg)
RewriteCond %{REQUEST_URI} !^/css/.*\.css
RewriteCond %{REQUEST_URI} !^/js/.*\.js
#rewrite all requests to index.php and get path (only intern)
RewriteRule ^/([^/]*)/(.*)$ /index.php?lang=$1&path=$2 [NC,L,QSA]
Thanks in advance
the files were missing ... so it redirected to the home.
It works better then me

Virtual host setup < cgi script

The issue
I'm writing a cgi script in c++. All works well as long as I go in to the precise url: http://localhost:90/joppli.bot
Now I'm trying to make a simple redirect to from everything to my cgi script. eg:
http://localhost:90/
http://localhost:90/foo
http://localhost:90/foo/bar
...should all render the same content as entering http://localhost:90/joppli.bot
Files
.htaccess
RewriteEngine On
RewriteRule (.+)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ joppli.bot [NC,L]
virtual host
<VirtualHost *:90>
DocumentRoot /var/www/joppli-bot
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so
SetHandler fastcgi-script
<Directory "/var/www/joppli-bot">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/www/joppli-bot/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/www/joppli-bot/log/apache2/access.log combined
</VirtualHost>
files at /var/www/joppli-bot
.htaccess
joppli.bot -> /home/erik/NetBeansProjects/joppli.bot/dist/Debug/GNU-Linux-x86/joppli.bot
log
+ apache2
+ access.log
+ error.log
Thanks to Justin Iurman Who wrote the following as a comment, witch solved most of it:
In <Directory "/var/www/joppli-bot"> block, replace AllowOverride None
by AllowOverride All (otherwise your htaccess is disabled)
I also added DirectoryIndex joppli.bot at the bottom of my .htaccess file to route all trafic from root directory to the script as well..

Remove index.php from Laravel 4 URL and Rewrite www to non-www

I have tried several ways to remove index.php from my URL (http://www.example.com/index.php/login), as well as redirecting www to the non-www URL.
Here is my default Apache VirtualHost file:
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/public/
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now I have added the following in to this file:
Redirect 301 / http://example.com
But this has not worked, I just get a redirect loop message. I also have the following .htaccess:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
To which I've added:
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But this does not work either.
The other issue is that for all of my routes I have to include index.php, like http://example.com/index.php/test
It's as if my .htaccess file is being ignored.
The server is a DigitalOcean Droplet running Ubuntu 13.04 x64. It has PHP 5.4.9 and Apache 2.2.22.
Any help would be very much appreciated. Thanks.
Thanks to anlutro in #laravel IRC chat, he pointed out that mod_rewrite may not be enabled. Usually this is on by default in all the servers I've ever worked with so it didn't click, as I've never had to enable it but on this DigitalOcean server, it wasn't.
I used:
a2enmod rewrite to enable it and then I restarted apache. All is good.
Cheers

403 forbidden using fastcgi

Trying to set up a ReviewBoard server, using apache 2.2 with fastcgi on Windows 7 (no, Linux isn't an option). I'm using (basically) the default httpd.conf, with mod_fcgid and mod_rewrite loaded, and
Include C:\mars\reviews\conf\apache-fastcgi.conf added at the bottom. This too is the default, excepting the Order, Allow, and +Indexes. The trouble is, when I attempt to load the site, I get 403 for / (which, according to the rewrite rule, would become reviewboard.fcgi). I added +Indexes to prove that yes, in fact, I can access those directories, so it doesn`t appear to be a permissions problem.
When I add ExecCgi, those 403 errors turn into 404 errors! I'm at my wits end, and over my head.
The contents of C:\mars\reviews\conf\apache-fastcgi.conf (with ServerName redacted):
<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
</IfModule>
<IfModule mod_fastcgi.c>
AddHandler fastcgi-script .fcgi
FastCGIServer "c:/mars/reviews/htdocs/reviewboard.fcgi" -socket "c:/mars/reviews/tmp/fastcgi.sock"
</IfModule>
<VirtualHost *:80>
ServerName #redacted
DocumentRoot "c:/mars/reviews/htdocs"
# Alias static media requests to filesystem
Alias /media "c:/mars/reviews/htdocs/media"
Alias /errordocs "c:/mars/reviews/htdocs/errordocs"
# Error handlers
ErrorDocument 500 /errordocs/500.html
<Directory "c:/mars/reviews/htdocs">
Options +Indexes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# Direct all other requests to the fastcgi server
RewriteEngine on
<IfModule mod_fcgid.c>
RewriteRule ^/(media.*)$ /$1 [QSA,L,PT]
RewriteRule ^/(errordocs.*)$ /$1 [QSA,L,PT]
</IfModule>
<IfModule mod_fastcgi.c>
RewriteRule ^/(media.*)$ /$1 [QSA,L,PT]
RewriteRule ^/(errordocs.*)$ /$1 [QSA,L,PT]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ /reviewboard.fcgi/$1 [QSA,L]
</VirtualHost>
What on Earth is going wrong here??
It seems to me, that you have mod_fcgid installed, but FastCGIServer-statement is in mod_fastcgi-section. I'd started here.
After speaking with Christian Hammond at ReviewBoard, he pointed me to this django ticket. It turns out that django doesn't support FastCGI on Windows!