How to force htaccess to without www url - .htaccess

My htaccess right now:
# Use PHP 5.3
AddType application/x-httpd-php53 .php
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [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]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
I need to do a rule, that if somebody type http://www.mypage.com/blog/blablatitle
it redirects him to http://mypage.com/blog/blablatitle
and the same for the frontpage and all pages.
I am using CodeIgniter, but I think this is not so important when dealing with htaccess.
This suggested solution is not working well in CodeIgniter:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Because it adds index.php in the address e.g.: when I type:
http://www.mypage.com/blog/blablatitle
it redirects me to:
http://mypage.com/index.php?/blog/blablatitle
And I need:
http://mypage.com/blog/blablatitle
Without this rule it is ok (without index,php) e.g.: http://mypage.com/blog/blablatitle
So, any advice how to improve this redirect rule som index.php doesn't appear in the redirected link?

SOLVED! ;) You need to put it right after
RewriteBase /

Try this please:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^index.php(.*)$ http://%1/$1 [R=301,L]

Related

codeigniter htaccess 301

Hi all I have the normal htaccess file for codeigniter and I want to 301 one url to another
I've had to obfuscate the links to be able to post :(
ie h2tp://www domain com/controller/method/value1
->h2tp://www domain com/controller/method/value2
but I'm having a hell of a time...
Here's the htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(home(/index)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
#Removes trailing slashes
#had to remove ajaxquery search else it fails
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(search/ajaxsearch)
RewriteRule ^(.+)/$ $1 [L,R=301]
#Rewrite all non-www to www based filenames
#should get rid of any canonical issues
RewriteCond %{HTTP_HOST} ^domain\.es [NC]
RewriteRule ^(.*)$ http://www.domain\.es/$1 [R=301,L]
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [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]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>[/code][/quote]
If I put
Redirect 301 /controller/method/value1 h2tp://www domain com/controller/method/value2
I get this
h2tp://www domain com/controller/method/value2?controller/method/value1
I've been hours on this now, it's killing me :)
Due to my complete lack of knowledge on htaccess I was trying to add the 301 redirects below these lines.
#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]
All checks had been run by then and the [L] flag was stopping anymore checks being run.
so when I posted a normal 301 redirect above the following lines it worked.
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

.htaccess 301 Redirect that Works for http and https cases, .com and localhost

I need to do a 301 redirect on a single page. If I just wanted it to work from www.example.com, I would have:
redirect 301 /urisegment1/urisegment2/urisegment3 http://www.example.com/urisegment1/urisegment2/NEWURISEGMENT3
The problem is, users may access http://www.example.com/urisegment1/urisegment2/urisegment3 or sometimes https://www.example.com/urisegment1/urisegment2/urisegment3/ if they have been logged in and access SSL-protected pages. Also I have the site set up locally so I can access it through http://www.example.local/urisegment1/urisegment2/urisegment3
How can I get this to work for all cases?
(That's the end of the question, my current .htaccess file below in case it is relevant. Note it's mostly not my own work, it is drawn from various sources).
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#Removes trailing slashes (prevents SEO duplicate content issues)
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+)/$ $1 [L,R=301]
#forces www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#force homepage to http from https
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{HTTPS} on
RewriteRule ^$ http://www.example.com/ [R=301,L]
#If a controller can't be found - then issue a 404 error from PHP
ErrorDocument 404 /404.html
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^sys.*
RewriteRule ^(.*)$ /index.php/$1 [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. The first condition allows access to assets, css, js and the robots file
RewriteCond $1 !^(index\.php|assets|css|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
I just tried the following rule
RewriteCond %{REQUEST_URI} !newvalue/?$
RewriteRule ^(.*)/[^/]+/?$ /$1/newvalue [L,R=301]
which should keep everything but the last URI segment, so it should work with http(s) and different domains.
You have to update the newvalue in both lines to avoid endless redirects.

htaccess and httpd.conf configuraion to enable codeigniter working on ubuntu 12.04

I have installed ubuntu 12.04 and I have complete requirements(apache,mysql and php5) for running php projects. Projects which are not in codeigniter are working well. Within codeigniter , when I load the project the default controller loads the index_view but when I click to other controllers; they wont work with error and display error
Not Found The requested URL /academic/login was not found on this server.
I realized that the .htaccess wont work; then the /etc/apache2/httpd.conf file is empty
I would like to request a sample of basic configuration of httpd.conf file so that my htaccess file can work. If any suggestion that can help me to arrive at some results would be appreciated. Thanks.
My httaccess is given below
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(platform(/index)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(localhost|academic) [NC]
RewriteRule ^(.*)$ http://localhost/academic/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [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]
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
This trusty one has never done me wrong:
RewriteEngine On
RewriteBase /academic/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
ErrorDocument 404 /index.php

Codeigniter htaccess index.php site down

Codeigniter htaccess started problem suddenly even when no changes were made to any files. I can access
www.mysite.com/index.php/controller/method
but when I access
www.mysite.com/controller/method
it gives a general 404, not a codeigniter 404. Any help is appreciated. The site is down.
Here is my present htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [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
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
In this case i'm 100% sure your mod_rewrite is not working properly.Also make sure your .htacess file is in your root directory. A have this one working properly on my server may be this will help you :-
# rewrite rules
RewriteEngine On
# ditch index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Mod_rewrite to SHOW a redirect from a wildcard domain

I am so about to kick mod_rewrite to the curb. Talk about "doesn't play well with others." Are there any witchdoctors about that can help me with this?
I have wildcard dns set up to identify users. I would like to go from:
http://username.domain.com/foo/bar
to:
https://www.domain.com/qux/waldo/username/foo/bar
Is this possible?
I'm developing with codeigniter and it has it's own mod_rewrite directives in .htaccess already, to strip out the index.php.
Here's what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.idseal\.local.*$
RewriteRule (.*) /site/assign/$1
#Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [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]
</IfModule>
First line verify if we are on the main domain or not, second line will match if there is a www or not, if there is a www it will be held on %1 thus making no changes if there inst, the data will be into %2 and will rewrite just fine.
I am not 100% sure this will work out for your needs but give it a try and report it back and i will try to assist you with what i can.
RewriteEngine on
RewriteCond %{http_host} !^www.domain.com [NC]
RewriteCond %{http_host} ^(www.)?([^.]+).domain.com [NC]
RewriteRule (.*) https://www.domain.com/qux/waldo/%2/$1

Resources