Htaccess changing URL case if directory exists - php

The problem I am having is that when the URL contains a directory that exists, the URL is getting changed the the case of the file.
More specifically, this is what is happening:
When I go to domain.com/cron/xxx then in my PHP script, uri will be:
cron/xxx
But if the folder Cron exists, then uri will be:
Cron/xxx
Does anyone know what causes this to happen?
Here is my .htaccess file:
RewriteEngine On
#remove www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?uri=api/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?uri=$1 [L,QSA]

I'm not super versed in ModRewrite, but you could try:
RewriteMap lc int:tolower
and then use it in your rule like so:
RewriteRule (.*) ${lc:$1} [R=301,L]

Related

How to improve this .htaccess file?

I created the following .htaccess file and I am wondering if it could be improved:
RewriteEngine On
RewriteRule ^site$ ./site/ [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
I want to redirect visitors which enter www.domain.com/site/ to www.domain.com/main/ and all other requests to www.domain.com/404.php?url=... This works, except when the user doesn't enter a trailing slash (www.domain.com/site).
You should be able to do that with a /? like
RewriteEngine On
RewriteRule ^site$ ./site/? [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
For the first rule, you rewrite site to site/ and then rewrite site/ to main/. I would rather do it in one step, e.g.
RewriteRule ^site/?$ ./main/ [L,NC]
The second rule would stay as it is.
With the current rules, you only redirect nonexisting paths to 404.php. If this is what you want, everything is fine. If not, you should remove !-f and !-d conditions and add a condition to exclude main from being rewritten to 404
RewriteCond %{REQUEST_URI} !/main/ [NC]
Even though the substitution ./404.php is valid, I would write it as
RewriteRule ^(.*)$ 404.php?url=$1 [L,NC]

Codeigniter remove index.php and prefix the URL with www

I want to remove the default index.php that auto comes with Codeigniter.
I've been able to remove that with this code
RewriteRule ^(.*)$ index.php/$1 [L]
example.com/index.php/blog can now be accessed by example.com/blog
I later wanted to prefix the URL with a www i.e example.com/blog should redirect to www.example.com/blog with these rewrite rules
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
After adding this code above to the end of my .htaccess file, it began to misbehave.
If I enter www.example.com/blog into a URL bar it works fine but if I enter example.com/blog it redirects to www.example.com/index.php/blog
What I'm I doing wrong?
I want example.com/blog to redirect to www.example.com/blog
Note: I am using the Codeigniter framework.
Added: This code is just on top of the previous ones I have up. Maybe this is it's problem please HELP!!!
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
Try this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ example.com/$1 [L,R=301]
After much tricks and following the post given by #Tpojka I was able to come up with this
RewriteEngine On
RewriteBase /
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# no www -> www
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This guy was my problem
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
I would need help on how to exclude folders and some files like robot.txt file like I tried in the malfunctioning line.
This is the complete .htaccess file which I use personally:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

htaccess remove www and redirect to accessed url

I am using codeigniter framework i need to force to remove www from the url so I am using this code
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]
This code is forcing removal of www. but the problem is when a user access a link with www
eg:www.mydomain.com/framework/article/sometestarticle368/
It is redirecting to
www.mydomain.com/framework/
How can i fix this ?
Change the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]
Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Thanks

Htaccess: changes root and doesn't find files

I have a htaccess file that I am try to make a rewrite but it causes me multiple problems. First here is my htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2&%{QUERY_STRING}
RewriteRule ^login$ login.php?category=$1&%{QUERY_STRING} [L]
The first problem is that when I make a request like "mysite/foo/bar/", it does rewrite the query string in category and itemId but it also changes the base directory for my css files and Js files to /foo/bar/design/css/style.css
The second problem is that I have a few path like "login/","register/","users/", and when I try to load the corresponding file, it says:
The requested URL /login/ was not found on this server.
Thanks for the help!
First change your .htaccess like this:
RewriteEngine On
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## don't do anything
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?category=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]+)/?$ index.php?category=$1&itemId=$2 [L,QSA]
RewriteRule ^login/?$ login.php?category=$1&%{QUERY_STRING} [L,QSA]

is it possible to hide the hancoded index.php using htaccess

i have few hancoded links in my site ,some thing like this
<li><span>Bla bla bla</span></li>
My Question is ?Is it possible to remove thoose index.php using .htaccess(putting it into the root folder) by writting this in it
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
you can do It with jQuery,check every href attribute to match your pattern, and change it. It's not so pretty but anyway ,like:
$.each($('a') , function(key , value){
...
define your pattern here and change the url ...
$(value).attr('href' , newHref);
});
Yes, you remove "index.php" in ".htaccess".
In one of my yii sites i found this, first test it local...
RewriteEngine on
# redirect www to non-www (http & https)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

Categories