Weird behavior with .htaccess - .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
http://mydomain.ovh/write/my/object.html
http://mydomain.ovh/extract/my/object.html
http://mydomain.ovh/fill/your/object.html
http://mydomain.ovh/write/your/data.html
http://mydomain.ovh/extract/your/data.html
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 ?

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.

Related

.htaccess rewrite to same alias without infinite redirects

I have...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
So, /in --is-really--> /login.php
This much works great. We all can learn how to do this from: .htaccess redirect with alias url
But, I want it to also work in reverse...
If someone should enter /login.php into the address bar, I want it to change to /in.
So also, /login.php --rewrites-to--> /in
From this Answer to a different Question, I want to be ready for anything, using REQUEST_URI. So, my .htaccess file starts with this...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
That also works great.
But now, I want to add this rule (my Question here) for /in <--> /login.php both ways, just how / <--> /index.php already works with .htaccess (v2). So, I adopted the settings and added a second rule...
| .htaccess : (v3) —not working!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...but then /in and /login.php both cause an infinite redirect loop.
What's the right way to do this, still using REQUEST_URI, and still having both rewrite rules (for index.php and for login.php)?
These Questions did not help:
Rewrite rule to hide folder, doesn't work right without trailing slash
This is not about a trailing slash
Allow multiple IPs to access Wordpress Site Admin via .htaccess
This is not about IP-based access
Htaccess URLs redirects are working for http not all https
This is not about https vs http
Rewrite-rules issues : .htaccess
This is not about cleaning up the GET array in the URL
apache htaccess rewrite with alias
This is not about rewriting the host/domain, thereby preserving the path
rewrite htaccess causes infinite loop?
This is not about www subdomain rewrites
.htaccess rewrite page with alias
This is not about rewriting "pretty" URLs nor about how to use slug settings in WordPress
Htaccess alias or rewrite confusion
This is not about simply having multiple rules with the same destination
htaccess rewrite to include #!
I'm not trying to rewrite #!
Reason of redirect loop is a missing RewriteCond %{ENV:REDIRECT_STATUS} ^$ before first redirect rule that removes index.php. Remember that RewriteCond is applicable to immediate next RewriteRule only.
Suggested .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
It won't cause redirect loop because after first rewrite to /login.php, variable REDIRECT_STATUS will become 200 and then the RewriteCond %{ENV:REDIRECT_STATUS} ^$ will stop redirect looping.
Thanks to the help from the user with the correct answer, I found that...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...doesn't go in .htaccess only once, but every time on the line before...
RewriteCond %{REQUEST_URI} ...

Redirecting using Rewriterule from .htaccess

I have a domain (let's say www.example.com) and would like this to point to /Example_folder/ of my server (within /var/www/).
So, if I try to goto www.example.com/images/test.html or example.com/images/test.html, it should be actually pointing at /Example_folder/images/test.html.
I tried to get this working using following code, but I can't figure out.
Trial#1:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/ [L]
If I use above code, I get ERR_TOO_MANY_REDIRECTS.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
Rewriterule ^(.*) /Example_Folder/index.html [L]
If I use above code (where index.html is specified), it would redirect but I can't get my domain to point at its subdirectories. (www.example.com/images/test.html would also point at www.example.com/index.html)
I got it working using the code from link below:
htaccess Silent Redirect to Subdirectory: Subdirectory showing when no trailing '/'
Last thing that remains is that when I point to www.example.com/Example_Folder, I want the address bar to show www.example.com, but I have not figure that out yet.
You can use this negative lookahead based rule to avoid rewrite loop:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
Rewriterule ^((?!Example_Folder/).+)$ /Example_Folder/$1 [L,NC]
Which means rewrite only if URI doesn't already start with Example_Folder/.

rewrite url with domain as condition

I'm using apache with mod_rewrite and codeigniter to do my routing.
I've got two seperate (wildcard)domains pointing to my website, say: *.foo.com and *.bar.com
*.foo.com/somepath should to rewrite to *.foo.com/index.php/somepath
and *.bar.com.somepath should to rewrite to *.bar.com/index.php/bar/somepath
This is my .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} [^.]{3}\.bar\.com [NC]
RewriteCond $1 !(index\.php|assets|favicon|bar)
RewriteRule ^(.*)$ /index.php/pzdossier/$1 [L]
RewriteCond $1 !(index\.php|assets|favicon)
RewriteRule ^(.*)$ /index.php/$1 [L]
This gives me the correct result when I go to dev.foo.com/whatever
but when I go to dev.bar.com/whatever it just rewrites to dev.bar.com/index.php/whatever instead of pzdossier.
If I change
RewriteRule ^(.*)$ /index.php/pzdossier/$1 [L]
to
RewriteRule ^(.*)$ /index.php/pzdossier/$1 [L,R=302]
It works, I get redirected to /index.php/pzdossier/whatever.
What am I doing wrong? why does a redirect work, but a rewrite doesnt?
Codeigniter was using the $_SERVER['request_info'] to determine what controller to call.
When I changed that to $_SERVER['PATH_INFO'] it correctly using the .htaccess rules as I wanted.
So change application/config/config.php $config['uri_protocol'] to PATH_INFO

htaccess rewrite if no rewrite was provided

First off I'm not an expert in htaccess stuff, but i try to accomplish the following for some SEO fixes.
When the url is loaded, without any params/rewrite it should get some data attached before it continues.
For example:
http://www.domain.com >>> http://www.domain.com/en/
I thought the rewrite was right like this, but didn't work (500)
RewriteCond %{REQUEST_URI} !^(en|nl|de|etc)$
RewriteRule ^(.*)$ /en/ [L,R=301]
added
RewriteRule ^(/?[^/]+) /index.php?rewrite=1 [L] # tell php we got a rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1/en/ [NC]

Redirect all pages except one page to sub-domain htaccess

I have a index.php in my main domain root
domain.com/index.php
And ive moved my forums which was in the same "root" to a subdomain
forums.domain.com
I need to Redirect everything except the index.php
ive tryed loads of ways and none seem to work
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !index\.php$
RewriteRule ^(.*)$ http://forums.domain.com [L,R]
RewriteEngine On
RewriteCond %{HTTP_HOST} animelon\.com [NC]
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(.*)$ http://forums.domain.com/$1 [R=301,L]
If anyone has any ideas that would be great
as for the above codes I would them googling about.
Cheers
You may use RedirectMatch instead of rewriting, that is, replace all the rewrite block you are showing with:
RedirectMatch ^(/(?!index\.php).*) http://forums.domain.com$1
You can see the full explanation of the regex on Regexr here. In brief, it sends all the URIs NOT beginning with /index.php to forums.domain.com.
If you don't need any other rewrite rule, you can turn off rewriting by removing all the lines beginning with "Rewrite" from your .htaccess.

Resources