Search code examples
.htaccessweb-hostingovh

Weird behavior with .htaccess


I'm new in rewrite rule and I encounter a little issue with my .htaccess

RewriteEngine On
RewriteRule ^toto\.html$ /toto.php [NC,L]
RewriteCond %{REQUEST_URI} !^/(my|your)/template\.html [NC]
RewriteCond %{REQUEST_URI} !^/(my|your)/example\.html [NC]
RewriteRule ^([^/]*)/([^/]*)/([^\.]*)\.html$ /$1.php?type=$2&name=$3 [NC,L]
RewriteRule ^([^/]*)/([^\.]*)\.html$ /index.php?type=$1&name=$2 [L]
RedirectMatch 404 \.(htaccess|htpasswd|ini|log|sh|inc|bak|bkp|sql|json)$

I tested it on https://htaccess.madewithlove.be/ which always give me the right rewrite.

When I test it on my webhosting :

I go to http://mydomain.ovh/my/object.html

The rewrite is right : http://mydomain.ovh/index.php?type=my&name=object

But when I go to

The rewrite is wrong : The requested URL /redirect:.php was not found on this server.

I don't understand this behavior...

So I tested new rewrite rules making a common script for write/fill action :

RewriteEngine On
RewriteRule ^toto\.html$ /toto.php [NC,L]
RewriteCond %{REQUEST_URI} ^/extract/(my|your)/[^\.]+\.html$ [NC]
RewriteRule ^([^/]*)/([^/]*)/([^\.]*)\.html$ /extract.php?type=$2&name=$3 [L]
RewriteCond %{REQUEST_URI} !^/extract.* [NC]
RewriteCond %{REQUEST_URI} !^/(my|your)/template\.html [NC]
RewriteCond %{REQUEST_URI} !^/(my|your)/example\.html [NC]
RewriteRule ^([^/]*)/([^/]*)/([^\.]*)\.html$ /set.php?type=$2&name=$3 [NC,L]
RewriteRule ^([^/]*)/([^\.]*)\.html$ /index.php?type=$1&name=$2 [L]
RedirectMatch 404 \.(htaccess|htpasswd|ini|log|sh|inc|bak|bkp|sql|json)$

I tested it on https://htaccess.madewithlove.be/ which always give me the right rewrite.

And when I test it on my webhosting :

I go to http://mydomain.ovh/my/object.html

The rewrite is right : http://mydomain.ovh/index.php?type=my&name=object

I go to http://mydomain.ovh/fill/my/object.html or http://mydomain.ovh/write/your/data.html

The rewrite is right : http://mydomain.ovh/set.php?type=my&name=object or http://mydomain.ovh/set.php?type=your&name=data

But when I go to http://mydomain.ovh/extract/my/object.html or http://mydomain.ovh/extract/your/data.html

The rewrite is always wrong : the request reach the script but without query string... (and it appear to be a redirect?)

["PATH_TRANSLATED"]=>
string(19) "redirect:/index.php"
["PATH_INFO"]=>
string(30) "/my/object.html"
["SCRIPT_NAME"]=>
string(11) "/extract.php"
["REQUEST_URI"]=>
string(37) "/extract/my/object.html"
["QUERY_STRING"]=>
string(0) ""
["PHP_SELF"]=>
string(41) "/extract.php/my/object.html"

Can someone help with these rewrite rules ?


Solution

  • You need to turn off option MultiViews at top of your .htaccess which it seems is turned on for your website in Apache config.

    Add this at top of your .htaccess:

    Options -MultiViews
    

    Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.

    Once you make this change, test in a new browser to avoid old browser cache.