CodeIgniter index.php removal htaccess causing mis-redirection - apache

I'm using following htaccess rule which is proposed all over internet for removing index.php in codeigniter urls. And there are some of the redirection rules i added above it.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
The problem is:
I'm getting some odd http request, which i think is caused by htaccess rules above.
Here are some of them :
https://www.sitename.com/index.php/favicon.ico
which ought to be .com/favicon.ico
https://www.sitename.com/index.php/scripts/jquery.js
which ought to be .com/public/jquery.js
as a side note im using base tag to redirect assets to /public/
strange thing is i couldnt find where the second redirection happens
i tested whole site and javascript & css files load correctly
i handled this redirections by making my controller ignore those requests but a while ago
a strange error happened. which i asked here:
https://stackoverflow.com/questions/11775849/htaccess-didnt-work-until-renaming-and-then-renaming-back
my guess is that, even though i ignore misredirected request, hosting company receives them and probably it was causing some trouble for them which led to the problem i shared in linked question by a maintenance of hosting company.
Anyway, Question is:
How can i make htaccess rule only redirect requests that doesnt have file extension at the end?

You're missing a line before the REQUEST_FILENAME line:
RewriteCond $1 !^(index\.php|assets|something|fav\.ico|robots\.txt)
You can modify this to suit your needs. I've got my JS/CSS etc in the assets folder so it's not affected by the rewrite.
so the full .htaccess will look like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !^(index\.php|assets|something|fav\.ico|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

Related

Adding exceptions to mod_rewrite

I got my basic redirects work with the mod_rewrite module. When requesting pages e.g. localhost/home it's correctly redirecting to localhost/index.php?page=home, but I have a problem with exceptions.
I created a folder api where I store files by category e.g. api/auth/register.php and api/customer/create.php. I tried to make rewrite rule that contains 2 params (in this example auth and customer) so basically it just drops the .php off from the url.
The rule that I made is following
RewriteRule ^api/(.*)/(.*)/?$ api/$1/$2.php [L]
After adding that line to my .htaccess, problems started to occur. For example my .css and .js files started to redirect. So maybe I need to make some exeption for the apis? Have you some other ideas to improve my rewrite rules?
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)/(.*)/?$ api/$1/$2.php [L] # problems started to occur after adding this line
RewriteRule (.*) index.php?page=$1 [L,QSA]
Thanks in advance.
RewriteCond will affect only the first following RewriteRule so you need the keep them next to your initial Rule, and move the added one above them (with its own conditions).
Also, your /api rule is not strict enough ((.*) will pick anything, including the slashes), which might not matter in you case, but still. I sugest you try with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([^/]*)/([^/]*)/?$ api/$1/$2.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$1 [L,QSA]

apache redirect to naked (non-www) domain messes up pages handler

all the urls in my website actually go through a PHP page that handles them by the page GET parameter (i.e. domain.com/sub/test is actually domain.com/page_handler.php?page=sub/test).
files or directories that exist don't go through the handler.
I've been trying to 301 redirect all www.domain.com requests to domain.com for improving SEO etc.
the problem is that this doesn't seem to work, no matter what rule I use and where I put it. this is the .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#THIS IS THE DISCUSSED RULE:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule (.*[^\/])$ page_handler.php?page=$1 [QSA,NC,L]
RewriteRule ^/?$ page_handler.php?page= [L,QSA]
DirectoryIndex page_handler.php?page=
When I put the rule in the current line, it works ok with pages that are supposed to go through the handler BUT it makes existing resources go through it as well (e.g. domain.com/page_handler.php?page=js/script.js) which is not good.
When I put it after the other rules it redirects www.domain.com/something to domain.com/?page=something.
So, the question is: how to redirect urls that begin with "www." to the naked (non-www) domains without affecting the other rules?
Thank you!
The problem with your code is that you are applying the first two conditions only to the non-www rule. Conditions can only be tested for the rule that immediately follows them.
So, you'll need to move those down, and clean up a bit:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /page_handler.php?page=$1 [QSA,NC,L]
If this causes issues for you, then you may want to change the way your page is detected, by using the REQUEST_URI instead of a $_GET['page']. If you want to do this (which is actually a better method), the last rule can be changed to the following:
RewriteRule ^ /page_handler.php [QSA,NC,L]

URLs redirecting to index.php when no protocol

Hoping someone with more htaccess experience can help us with this. We have Drupal 7 site that we have just moved from a dev to the live host (different hosting companies). However, now when someone puts a url with no protocol directly into the address bar (for example: examplesite.com/members), the page redirects to examplesite.com/index.php. I have been muddling around trying to fix this in the htaccess file, but have not been able to find the proper syntax for allowing urls with no protocol, while also forcing https://.
Our code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
We have tried moving the RewriteRule ^ index.php [L] line to underneath all rules, or commenting it out. This fixed the initial problem, but breaks the drupal admin functionality on the backend (can't see the admin menu, can't save anything, etc)
Any insight would be helpful, let me know if more info is needed. Thank you in advance.
You need to move the entire block, conditions + rule to the bottom. Drupal routes everything through the index.php script and it requires the previous 3 conditions in order to do that properly. If you simply move the RewriteRule line, then the conditions are all gone.
RewriteEngine On
# this needs to come first
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# then drupal stuff comes LAST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

.htaccess Subdirectory Rewrite with Jekyll

I have a small blog in generated with Jekyll up on a shared Apache server. I have been following this guide to set up the server so that I can update the site by pushing changes via GitHub, but I haven't even gotten that far yet. The .htaccess rewrite rule to point the domain to the generated /_site subdirectory has me stumped. The code I'm using (below) is redirecting the site correctly, but the guides I've read say that this code should hide the subdirectory from the URL, and this is not happening. It works correctly on the homepage, but subpages still have /_site/ in their URL. Any ideas?
My website
.htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?joejoiner.tk$ [NC]
RewriteCond %{REQUEST_URI} !^/_site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /_site/$1
RewriteCond %{HTTP_HOST} ^(www.)?joejoiner.tk$ [NC]
RewriteRule ^(/)?$ _site/index.html [L]
Add a trailing / to the end of line 6 in the above code block, like so:
RewriteRule ^(.*)$ /_site/$1/

.htaccess Rewrite Within Directory - Hide PHP extension and force trailing slash

I'm trying to hide the .php extension from my files as well as force a trailing slash on the resulting URLs.
Example: A request to /about.php would become /about/ and requests to /about would go to /about/.
The following rewrite code worked perfectly when I was in the root of my hostdomain:
RewriteEngine On
RewriteRule ^(.*)/$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://edit.mydomain.org/$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php\ HTTP/ [NC]
RewriteRule .+ http://edit.mydomain.org/%1 [R=301,QSA]
However, I need to move my files into a directory of this host name. Adding a directory name to the rules and having the .htaccess in the directory itself didn't work at all and seems to cause a endless redirect.
I looked around StackOverflow and other websites and tried numerous examples and ended up with many different errors with the most common being:
Everything is an endless redirect.
Everything except the directory home page is a 500 Error.
about.php redirects to /about but there's no redirect to /about/ and /about/ displays a 500 Error.
Everything working, but the home page (of the directory) index.php when accessed without a filename goes into an endless redirect.
Things redirect to edit.mydomain.org/home/username/public_html/mydomain.org/edit/pagename.php which obviously doesn't exist.
Thanks for any help! I really need to keep these files in a directory although the .htaccess could go into the host name root if its needed.
The directory for this would be edit.mydomain.org/dave/
Save this as a .htaccess and put it in the 'dave' directory
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://edit.mydomain.org/dave/$1/ [R=301,L]
RewriteRule ^(.*)/$ $1.php [L]
This works for me
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html