88888

htaccess force www and remove index.php

Question:

Currently I have htaccess which removes index.php from URL, its working fine. But now I want to force url to start with www.

My current code:

DirectoryIndex index.php RewriteEngine on RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

I tried following code, but its not working:

DirectoryIndex index.php RewriteEngine on RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{HTTP_HOST} !^$ RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTPS}s ^on(s)| #RewriteRule ^(.*)$ /index.php?/$1 [L] RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI}/index.php?/$1 [R=301,L]

But its not working, I am using codeigniter

Answer1:

Change order of your rule and keep redirects before rewrite rules:

DirectoryIndex index.php 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,NE] RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

Recommend

  • Codeigniter Htaccess and URL Redirect issues
  • Not showing 403 code in status (200 OK) and some page
  • .htaccess not redirecting to error page
  • How can I see the value of RewriteRule or RewriteCond server variables?
  • Symfony deployment on heroku points to app folder
  • my codeIgniter can't jump to the right address from the index.php
  • Server page priorities between index.php or index.html
  • multiple parameters friendly URL rewriteRule
  • mod_rewrite seems to ignore [L] flag
  • Why I am getting 'Forbidden' when I try to login on my live codeigniter site?
  • .htaccess mod_rewrite Unknown number of Variables of a GET form
  • Getting Zillow API Data
  • Redirecting codeigniter project to https with htaccess: never complete
  • Htaccess Public Folder Rewrite Rule
  • Help with mod_rewrite
  • How to save dynamically created textboxes and their values
  • Passing information to server-side function in a Google Docs Add On
  • How to apply a custom handlers to only specific folder
  • Streaming screenshots over WebRTC as a video stream from iOS
  • pip in virtualenv gets ConnectTimeoutError
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Moving Android View and preventing onDraw to be called over and over again
  • Installed module is empty
  • HTTP/2 streams vs HTTP/1.1 connections
  • Syntax for setting draggablecursor property in google maps api
  • req.body is undefined - nodejs
  • Modifying destination and filename of gulp-svg-sprite
  • htaccess rewriting URLs with multiple forward slashes
  • Importing jscolor library in angular 2
  • GridView Sorting works once only
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • PHP: When would you need the self:: keyword?
  • How to get icons for entities from eclipse?
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • reshape alternating columns in less time and using less memory
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?