Mod Rewrite with 2 similar rewrite cond - apache

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]

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!

RewriteRule with query string gets rewritten to /param/param/

I have this rewriterule:
RewriteRule ^gallery/([^/]+)/?$ /gallery.php?title=$1 [L]
However, when navigating to gallery/a/, the internal redirect is to :
gallery.php/a //(instead of: gallery.php?title=a)
What would cause this behavior?
Option MultiViews might be culprit here. 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.
Turn it off by using this line at top of your .htaccess:
Options -MultiViews

ModRewrite: RewriteRule fails

i'm trying to redirect two requests but only one of them works:
RewriteEngine On
RewriteRule ^cat/([^/\.]+)?$ cat.php?id=$1 [L]
RewriteRule ^user/([^/\.]+)?$ user.php?id=$1 [L]
domain.com/cat/something -> works
domain.com/user/12345 - > don't work ($_GET is empty)
It's very strange because the first (identically) rule works perfectly.
Maybe it's just because the second rule can't handle digits or something?
Thanks!
Most likely you have options MultiViews turned on. Disable it by placing this line on top of .htaccess:
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.

Any .htaccess rule results in a 500 error

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.