0

I like to remove index.php from the url with jquery

I try this code but continuously reload the page

$(document).ready(function(){
   var href=document.location.href;
   if(href.indexOf("index.php")){
       document.location.href = href.replace( "index.php", "" );
   }   
});

How to remove and dont continuously reload page ?

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
  • why exactly do you want to remove `index.php` from url? – dreamweiver Jun 19 '14 at 05:52
  • First of all indexOf returns -1 if not matched, not false. Then you need an Apache or Nginx (or what server do you use) redirects to remove index.php, not javascript. – pushOk Jun 19 '14 at 06:01

3 Answers3

2

You can use history object to achieve this.

history.pushState(null,pagetitle,chageUrL);

where pagetitle is the new page title if that requires to be changed and changeUrl is the new url you want to set

But remember this is not supported by IE v <8

Refer to below links for better understanding on this:

Modify the URL without reloading the page
http://www.w3schools.com/jsref/obj_history.asp
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

Community
  • 1
  • 1
abhinsit
  • 3,214
  • 4
  • 21
  • 26
1

Actually no need to jQuery here, raw JavaScript is enough

the problem with your code is you are comparing with -1 which is in JavaScript truly value

if(-1){ alert('i am here') /* the code here will be executed */}

and you can try this in your console: !!-1 // => true

so you have to check if its greater than -1 or 0 (since here index.php cannot be at the beginning of the string)

if(window.location.href.indexOf('index.php') > -1) // or 0 
window.location.href = window.location.href.replace('index.php', '');
amd
  • 20,637
  • 6
  • 49
  • 67
0

I would say its a wrong logic to do it on the client side. In fact you cant(should'nt) edit the url for cosmetic purposes on the client side.

You can do this with an htaccess mod on the Server Side (ASSUMING YOU HAVE AN APACHE SERVER, SINCE YOU ARE EDITING A PHP PAGE IN THE URL). Add the below line to your htaccess file in the server directory(where your page is situated)

for example in www.example.com/test, put the below line in the test directory ('.htaccess') file

DirectoryIndex index.php index.html site-down.php

so when you go to www.example.com/test you get the index.php page.

Don't edit URL's on client side ! This is not a good practice as well as not good for SEO.
Even if you have some different server like nGinx / ISIS etc.. Do it properly on the server side where it should be done ! I can undertand if it was for removing an "#id" etc.. from the url
for which your existing code is appropriate. But editing target pages is a no no !

I recommend you to read an existing stackOverflow page..
Apache friendly urls
to create true user+seo friendly url's.

Community
  • 1
  • 1
Vinodh
  • 41
  • 5
  • the provided solution doesn't remove the index.php from url if typed explicitly or linked from an external link. I dosen't agree with you, this will not affect the SEO – amd Jun 19 '14 at 06:09
  • for eg, you have a page www.example.com/test/index.php you just removed index.php clientside through jquery so it reads www.example.com/test/ what happens if the visitor types this in another window..? will your page open..? SEO is just a bonus by doing such cosmetics on the server side.. dont take it as a token to comment ! – Vinodh Jun 19 '14 at 06:15