Bruno
Bruno

Reputation: 71

URL without www to www - using mod_rewrite and CodeIgniter

I'm maintaining a website which was in most part developed by another developers team. I'm developing new functionalities, but sometimes I don't know how exactly some old code works. The website was developed using CodeIgniter and there is a simple .htaccess file containing a single rule. I'm not that good at PHP or mod_rewrite, so I need your help please.

In the .htaccess file there is these lines:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

I was searching Google about mod_rewrite and I suppose the code above checks if what is being requested is not a file or directory, and if not, it rewrites the URL inserting index.php?/ after the domain. So for example if I request http://www.example.net/teste it would be rewritten as http://www.example.net/index.php?/teste. am I right? I also suppose it was done to match the CodeIgniter's URL format that includes the index.php from the framework, right? But what I think is curious is that I'm not able to see the index.php?/ in the URL even after the RewriteRule terminate executing(but that's fine, it is exactly what I want).

My problem now is that I want to redirect all URL without www to a URL with www. So, I tried to insert these 2 lines in the final of the .htaccess:

RewriteCond %{HTTP_HOST} ^example.net
RewriteRule ^ http://www.example.net%{REQUEST_URI} [R=301]

It works, but now I can see the index.php?/ part in URL. I don't want the index.php?/ part to be shown.

am I doing something wrong? Do you know a way to include the www in the URL without showing the index.php?/ part.

Thank you.

==================

UPDATE with answer:

Hey guys,

I modified my .htaccess to appear like that:

RewriteEngine On
RewriteBase /

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{HTTP_HOST} ^vesteer\.com\.br$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

It worked. Everything's OK now.

Thank you for all replies.

Upvotes: 0

Views: 210

Answers (1)

Softbazz
Softbazz

Reputation: 202

Ok find your solution, just update you .htaccess codes

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

Upvotes: 1

Related Questions