0

Using Htaccess, I am able to remove the www from the url but im struggling endlessly with completely removing the index.php.

I am using the codeigniter framework and I checked my config file.

$config['index_page'] = '';
$config['base_url'] = '';

Ive also tried: as someone suggested elsewhere on S.O:

$config['base_url'] = '/';

My htaccess file looked like this:
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L]

#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [L,R=301]

Ive tried all the different versions of writing the mode-rewrite rule, this one works the best but it adds a question mark to the URL and a forward slash RewriteEngine On

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

The result at the moment, if i type www.mywebsite.com, it will change to mywebsite.com, which is correct.

But if I type www.mywebsite.com/someuri then it becomes www.mywebsite.com/index.php?someuri

with the last modrewrite rule the url becomes www.mywebsite.com/?/someuri

I then tried adding the question mark after index.php like this:

# Now the CodeIgniter part
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

With the latter, if i type index.php into the URL, it doesnt go away.

I clearly dont understand mod-rewrite, can someone please guide me in the right direction.

5
  • Probably because you have [L,R=301] in your forcing non-www rule. The L indicates last rule, so don't process any further. Try removing that from there. I can't test it at the moment for it to be an answer, I'm not at a machine with Apache installed. Commented Apr 18, 2017 at 15:40
  • You might find this htaccess tester really handy http://htaccess.madewithlove.be/ Commented Apr 18, 2017 at 15:56
  • I removed [L] the result is still the same. It was worth a try though Commented Apr 18, 2017 at 16:37
  • 1
    Don;t leave base url blank Commented Apr 18, 2017 at 21:43
  • Possible duplicate of php echo base_url() bringing me to 404 Commented Apr 19, 2017 at 2:13

2 Answers 2

0

Try the following

Open config.php and do following replaces

$config['index_page'] = "index.php"

to

$config['index_page'] = ""

In some cases the default setting for uri_protocol does not work properly. Just replace

$config['uri_protocol'] ="AUTO"

by

$config['uri_protocol'] = "REQUEST_URI"

HTACCESS

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
Sign up to request clarification or add additional context in comments.

3 Comments

Also $config['base_url'] Must be set. e.g. $config['base_url'] = 'http://mywebsite.com/'; (include the trailing slash)
Thank you, the solution you suggestion is one which ive tried before. It works. but if I add. www to the url then index.php? comes up. Also if i manually insert index.php in the URL then it remains, it does not get removed.
Codeigniter 3 versions do not have AUTO for uri_protocol
0

Try the following Open config.php and do following replaces

$config['index_page'] = '';

to

$config['index_page'] = 'index.php';

config base_url

$config['base_url'] = '';

to

$ark_root           = "http://".$_SERVER['HTTP_HOST'];
$ark_root          .= str_replace(basename($_SERVER['SCRIPT_NAME']),"", $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $ark_root;

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.