0

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....

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

3 Comments

i pretty much want to get rid of the index.php from any reference of ever showing up in the URL bar ;_)
@s2xi why are you linking to /index.php then? Why don't you just link to /? The redirect flag (R=302) should solve your problem in either case.
I'm not linking, but I wanted to see if in the event that I typed in www.example.com/index.php that index.php will be removed.
0
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.

5 Comments

the js is used in this context $(window).bind('hashchange')... let me try the RewriteRule
heh, its an odd i tried the js method and it kept on looping my site
Because js removes index.php from URL and your third rule RewriteRule ^(.*)$ /index.php?/$1 [L] adds it back.
yes, I thought of that :_) I had removed the htaccess before trying the js method. Thats why i was a little confused
Have you wrapped code for location into some if condition? Otherwise js will execute that code evey page load, which result in problem, you describe.

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.