Eli
Eli

Reputation: 4359

can't remove index.php no matter what i try, htaccess or javascript method

So I have been trying to remove the index.php from my url and haven't been successful

I have tried these methods

.htaccess

RewriteEngine On

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

javascript

window.location.href.replace('index.php', '');

my url looks like this still www.example.com/index.php

My site works if I go to www.example.com and loads in the index.php and won't show it, thats normal behavior. But if I type in it in with the /index.php the js or htaccess won't erase and just show www.example.com

I'm not sure what to try....

Upvotes: 0

Views: 343

Answers (2)

samshull
samshull

Reputation: 2328

From your question I am assuming that you want to redirect to example.com/ from example.com/index.php?

Redirect to root:

RewriteRule ^index.php$ / [R=302,L]

Upvotes: 1

kirilloid
kirilloid

Reputation: 14304

RewriteRule ^(.*)$ /index.php?/$1 [L]

Actually does not remove index.php from URL, but adds it. You should use something like that:

RewriteRule ^index\.php\?(.*)$ /$1 [L]

As for javascript location.href is a string and string's method replace returns changed value, it doesn't modify the string in-place. Thus in js you can use something like this:

location = location.href.replace('index.php', '');

window may be ommited unless you use this code in some local context, where another variable with name location exists. And location = ... is equivalent to location.href = ...
If you want current URL (with "index.php") not to be saved in browsing history, you may use this:

var newURL = location.href.replace('index.php', '');
location.replace(newURL);

Again: location.href is a string, while location itself is an object. It's replace method is a different thing.
https://developer.mozilla.org/en/DOM/window.location#Methods

In general I'd prefer rewrite for this, but https://serverfault.com/ is better place to ask about rewrite rules.

Upvotes: 0

Related Questions