Any .htaccess rule results in a 500 error - apache

I have an .htaccess problem on my localhost Apache server. If I put anything in the .htaccess file, I get an Internal Server Error. I'm trying
Options +FollowSymlinks
or
RewriteEngine on
or
RewriteRule (.*)\.htm $1.php
But they all return the same error.

Likely your AllowOverride setting does not include neither Options nor FileInfo.

Related

.htaccess not working on SSL apache

Trying to fix this .htaccess, so it can work on a SSL apache server. Before it was hosted on another Linux server (http://) and was working without problems, but when uploading the files to another Linux server with apache and SSL (https://), it stopped working. The main function is to hide the .php extension...
HereĀ“s what I was using:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
Thanks for your help!
So, .htaccess files may not be allowed, and by default on many systems they are not allowed. To see if .htaccess is allowed, make this your .htaccess:
BREAK IT!
That should be the only contents in your .htaccess. Attempt to load any page on your website, and if .htaccess usage is enabled, you would see "Internal Server Error" or possibly some other error, but you would not see your actual page.
If you do see the error, that's actually good and means .htaccess usage is enabled. If you don't see the error, it's likely that you will have to find your Apache .conf file and inside look for the line(s):
AllowOverride None
Change that to:
AllowOverride All
If after doing that you still can't use .htaccess, then there may be other apache related files that have "AllowOverride None". On that comes to mind is your virtual host file, and on my system that is located at /etc/apache2/sites-available/.
If you still have problems, check this out:
https://docs.bolt.cm/3.3/howto/making-sure-htaccess-works
Solution is for the issue, need to change in apache2.conf file after that it will works,
Change this file /etc/apache2/apache2.conf
update it same
OLD:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
New Updated Code:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
I had a similar problem. Apache 2.4.23 with .htaccess error 404 set up.
Using it with HTTP works fine; access it with HTTPS didn't work.
I seted AllowOverride All in the http.conf and error 404 wors well.

mod_rewrite all to index.php in one environment

I know there are a million of questions about this, but I've tried the solutions in other questions and haven't got it to work in my case. I'm trying to redirect everything to index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This works in my local environment, but in my staging environment it fails and I get a 404 error. I've checked that mod_rewrite is enabled with phpinfo(). I've tried changing a few things like using ^(.*)$ instead of ^ and using /index.php instead of index.php
What else could be the problem?
Figured it out. It was a problem with the configuration of apache. Whoever set it up for the site I'm working on did it like this in the site's .conf file:
<Directory "/data/path/to/directory">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# and a bunch of rewrite rules...
</Directory>
The AllowOverride None prohibits the use of .htaccess. In our local and development environments, the directory structure is different, so this code doesn't even get applied. That's why the .htaccess was working.
To solve the problem I'll either have to change the line mentioned above to AllowOverride All or add another configuration for the website I'm working on like the one above that does what I wanted to do with .htaccess.
Problems like this shouldn't come up for anyone if their environments are configured appropriately, but if anyone does run into a similar situation, check the configuration of the site in apache!

Apache redirect simple url to the same named php file

I'd like to redirect some static pages to the same named php file.
This is the code I'm using:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^services$ services.php [L,QSA]
</IfModule>
I'm getting 404 error when I open the "/services" url. If I change it to
RewriteRule ^services-anything$ services.php [L,QSA]
I can open "/service-anyting" without any problem.
Does anybody have any idea?
Thanks
Instead of your rewrite rules you can just enable MultiViews option:
Options +MultiViews
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

Enabling .htaccess for 302 redirects

I am attempting to create an .htaccess file on an Apache server to implement 302 redirects. Hosting is provided by BlueHost.
Right now I am testing an .htaccess file which consists of just a single line:
Redirect http://mydomain.com/?_escaped_fragment_=about http://mydomain.com/about.html
The file doesn't work. After learning more, I created an httpd.conf file, which looks like this:
<Directory />
Options FollowSymLinks
AllowOverride All
Allow from all
</Directory>
One of the answers to this question (Apache 302 Redirect) suggests the following may be at issue:
mod_alias is not loaded--how do I check whether it is or isn't?
Adding the httpd.conf file may require an Apache restart. Is this possible on a shared BlueHost server?
Any advice on getting the 302 redirects to work is much appreciated. Thank you!
You can't match against the query string in a redirect, you'll need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=about$
RewriteRule ^$ /about.html [L,R=302]

Mod Rewrite with 2 similar rewrite cond

so I want 2 different rewrite rules.
First:
http://mydomain.com/affiliate/533/1 -> http://mydomain.com/affiliate/index.php?member=533&campaign=1
Second:
http://mydomain.com/affiliate/533/2/1 -> http://mydomain.com/templates/2/step1.php?member=533
The following .htaccess gives 500 Internal Server error. What am I doing wrong?
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^affiliate/([0-9]+)/([0-9]+) affiliate/index.php?member=$1&campaign=$2 [NC]
RewriteRule ^affiliate/([0-9]+)/([0-9]+)/step([0-9])+) templates/$2/step$3.php?member=$1 [NC]
Just checked your example with a clean server install.
As long as I don't use Options +FollowSymlinks in .htaccess it works fine with only AllowOverride FileInfo.
When using Options +FollowSymlinks in .htaccess I need AllowOverride Options FileInfo in my <Directory > entry.
So, it seems not to be a bug.
I just checked your config and my error.log told me "RewriteEngine not allowed here".
After adding a "AllowOverride All" to that test directory the error went away.
However, the Apache manual at https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule says:
To enable the rewrite engine in this context, you need to set
"RewriteEngine On" and "Options FollowSymLinks" must be enabled. If
your administrator has disabled override of FollowSymLinks for a
user's directory, then you cannot use the rewrite engine. This
restriction is required for security reasons.
So the correct way would be setting up a and setting the FollowSymLinks option there.
Alex.
You just forgot the parenthesis, which, in turn, when Apache tries the last Regexp, generates an error, thus an internal error, which ouputs a 500. So here's the version that should work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^affiliate/([0-9]+)/([0-9]+) affiliate/index.php?member=$1&campaign=$2 [NC]
RewriteRule ^affiliate/([0-9]+)/([0-9]+)/step(([0-9])+) templates/$2/step$3.php?member=$1 [NC]