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

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

Related

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.

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.

mod_rewrite URI with same name as folder or files

A simple .htaccess file with this:
RewriteEngine On
RewriteBase /
RewriteRule ^webshop$ /index.php?page=webshop [L]
... does not work for me because a have a file called webshop.php in the web root. Renaming the file solves the poblem, and changing the regex in the .htaccess file solves the problem, but still - it's only a partial match of the file name...? The only thing I can find on this is to use
DirectorySlash off
I've tried that and it made no difference.
Need some help here, there must be a pretty simple solution to this.
Most likely you have MultiViews options enabled. 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.
Disable it by placing this line on top of your .htaccess:
Options -MultiViews

.htaccess rewrite url for missing reources

I would like to rewrite files that don't exist to a php handler. I am currently using this .htaccess file, but it doesn't work as I'd like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/index.php?_url=/$1
When I have no files, it works; it redirects correctly for resource that does not exist to my app, and I am able to capture it.
When I have exactly matching file (e.g.: test.txt and I request /test.txt); it loads test.txt correctly.
However, when I have a partial match (e.g.: test.txt exists, but I request /test); it does not redirect at all. In fact, it gives me the standard Apache 404. I want this to actually rewrite to my app, so I can deal with the request in a different manner.
I'm using pretty much default apache 2.2.22 from Debian. Is there some configuration I am missing, or is this the intended behaviour of the rewrite? Is there a way to achieve what I want?
I think you have MultiViews enabled in your Apache by default. Try adding this line on top of your .htaccess:
Options -MultiViews
With MultiViews Apache does its own rewrites and that usually conflicts with mod_rewrite

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]