Using NC in a .htaccess file not working - html

I am trying to create an .htaccess file that ignores the case sensitivity of my files. I have the following so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This works when I type the file name exactly. For example, one of my files is About.html. When I type in www.domain.com/About, it goes to the right place, but when I type in www.domain.com/about, it does not.
I have seen posts about people using [NC], but I cannot get that to work either. I have also tried putting
CaseInsensitive On
But that seems to break everything (couldn't even load the home page), so I took that bit out. Does anyone have any idea on how to accomplish this?

Case insensitivity does not work like that, it activates case insensitivity for the regular expression matching, not the files.
For example :
RewriteRule ^somefile.php$ /index.php
RewriteRule ^somefile.php$ /index2.php [NC]
if someone try to access http://domain.com/SomeFile.php, it will not match the first line but the second line will, this will redirect him to index2.php (and not index.php). But if you the index2.php file is named instead INDEX2.php, the redirection will throw a 404 not found error.
What you are looking for is using RewriteMap. you need to put this in you server configuration file :
RewriteMap lc int:tolower
Restart apache, and use a rule in your htacces like this :
RewriteRule ^(.*)$ /${lc:$1}
This will turn any request to lower case, your files then must be all lowercased.
More informations about RewriteMap => see documentation

Related

How can I do to limit a RewriteRule only for the current directory with .htaccess?

Let's assume that we're are talking about website.com and in the root directory there is a folder called tags.
This directory helps me to have "dynamic urls", for example: website.com/tags/my-dinamic-url or website.com/tags/another-example.
To do this, after an entire day of search :) I discovered that can be made by using .htaccess and at the moment it looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?t=$1 [QSA]
In my small point of view it is fantastic, can be funny for someone but after hours and hours of search on stackoverflow.com I finally understand something about the .htaccess file and about RewriteRule.
The problem is: Now, this RewriteRule continues endlessly because instead of just limit to website.com/tags/my-dinamic-url it continues also if I try something like website.com/tags/my-dinamic-url/continue/another-continue/endlessly.
How can I do to stop it? I need that if someone add a slash the page needs to return the 404 error.
So website.com/tags/my-dinamic-url need to work but website.com/tags/my-dinamic-url/ or website.com/tags/my-dinamic-url/something needs to return the 404 error.
The endless redirect can be prevented by checking if the uri starting with /tags. Assume the rewrite rule only apply when the uri is one level deep:
# pass if the uri not starting with /tags/*
RewriteCond %{REQUEST_URI} !^/tags/ [NC]
# redirect, ([^/]+) matches any characters, except / character (slash)
RewriteRule ^([^/]+)$ /tags/$1 [R,QSA,L]

The URL adds /public_html/ after changes in .htaccess

I'm a newby on using the .htaccess file for making changes on my website.
I want to change a URL automatically by adding the "www." at the begining of the domain name. To do so I enter the following code on the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.arnaldpedros.com$ [NC]
RewriteRule ^(.*)$ http://www.arnaldpedros.com/$1 [L,R=301]
I see allover the web that this is the proper way to do it but when I type arnaldpedros.com in my browser it takes me to:
http://www.arnaldpedros.com/public_html/
Whis is a 404 Not Found page, with the message:
Not Found
The requested URL /public_html/ was not found on this server.
What I want to do is to type arnaldpedros.com and to automatically go to www.arnaldpedros.com.
It's driving me crazy cause the same happens on many websites I mange.
Any help please?

webpage returning 403 error and not showing the actual page

I have a page on my website ( it's html ) which is named "providers.html".
in order to remove the .html part from url, I added this code to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
now the problem is that I have a folder which is named "providers" and contains files related to that page.
now when I go to the url, I get a 403 error because browser loads the folder and not the html page.
I really appreciate if any one can give me some guidance to solve this problem.
thanks
This might be what you're looking for:
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ([^\.]+) $1.html [NC,L]
Turned off directory slashes. This part is the most important part. Basically, if a directory exists, it shouldn't add a slash. This way, we can continue checking rules against exact URI input.
Added a check to see if the the current request does not end with a slash.
If it doesn't, rewrite to the respective HTML file
Otherwise, load up the contents of the directory. You'll more than likely want to have another .htaccess file in there to prevent listings etc., and only allow access to existing files.
Update: Removed ^ and $ from the rule, as I don't believe it is necessary here.

Htaccess file does not change file extensions

I am trying to remove the .html extensions of my website when navigating to another page on my website but the extensions are still there after modifying my .htaccess file.
Example:
Current look: example.com/index.html
How I want it: example.com/index
Inside File:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Below I have pasted the url rewriting code necessary to chop off extensions manually, which should be in .htaccess file in the directory of your website that you would like to rewrite the extensions for.
RewriteEngine On
RewriteRule horses.htm Xu8JuefAtua.htm
This simply displays the horses.htm page as Xu8JuefAtua.htm in the address bar.
To rewrite a url to not need the file extension and be case insensitive, then this is the code you are looking for:
RewriteEngine On
RewriteRule ^horses/?$ Xu8JuefAtua.html [NC]
The regular expression ^ means the beginning of the URL path, the ? means that the preceding character, in this case a slash in not necessary. The $ matches the end of the url.
Smashing Magazine gives a great writeup on the .htaccess file and how to use Regular expressions to do all kinds of things, check out their post.

Redirect request for directory-name to directory-name.html with htaccess

On my website, when somebody requests a directory, I want it to remove the '/' and add '.html' (minus the quotes, of course).
Example:
If someone goes to domain.com/directory/ it should redirect to domain.com/directory.html/ and the same should stand for: domain.com/another-directory/ should redirect to domain.com/another-directory.html.
I would like to place a line (or two) of code to my htaccess file that will make any directory (URL ending with /) redirect to URL.html (removing the / of course).
I would also like it to visually redirect, so the user will actually see it change to .html.
I'm quite a novice web programmer, and any help is greatly appreciated.
Note: I did use Redirect /directory /directory.html and that worked, but that requires a lot of extra coding, and I would much prefer one simple statement to cover all directories.
This is going to be a bit difficult with htaccess, I assume you want to do the following:
If someone accesses a directory that isn't the root (simply http://domain.com/), redirect them to the directory name ending with .html
After the get redirected, internally rewrite the .html back to the directory so apache can serve the directory.
First one is straightforward:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to make sure the request is for a directory that exists
RewriteCond %{REQUEST_FILENAME} -d
# rewrite the directory to
RewriteRule ^(.+)/$ /$1.html [R]
Second part is tricky
# check to make sure the request IS for an html file
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# See if the directory exists if you strip off the .html
RewriteCond %{DOCUMENT_ROOT}/%2 -d
# Check for an internal rewrite token that we add
RewriteCond %{QUERY_STRING} !r=n
# if no token, rewrite and add token (so that directories with index.html won't get looped)
RewriteRule ^(.+)\.html /$1/?r=n [L,QSA]
However, if what you just have is a bunch of files called directory.html, directory2.html, directory3.html, etc. and you want to make it so when someone enters http://domain.com/directory2/ into their address bar they get served the contents of directory2.html, it will be much simpler:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to see if the html file exists (need to do this to strip off the trailing /)
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
# rewrite
RewriteRule ^(.+)/$ /$1.html [L]