R. David
R. David

Reputation: 80

How to change the url address using jquery/JS

Its like My current url address is

http://localhost/foldername/index.php

and i wanna show this as http://localhost/foldername is it possible with jquery/JS without using .htaccess

Upvotes: 1

Views: 826

Answers (2)

IMSoP
IMSoP

Reputation: 97977

There are two parts to making a pretty URL:

  1. if you load the "ugly" URL, redirect to the "pretty" URL
  2. if you load the "pretty" URL, serve the right content

JS can help with (1), because it can see the current URL, and send the browser somewhere else.

For (2), though, you need something on the server-side, because when the browser requests http://localhost/categoryname, the server needs to know what to send it.

This is what mod_rewrite is for. Note that .htaccess is just a file you can put Apache configuration in, not the name of the configuration itself; it's commonly used on shared hosts where you don't have access to the core Apache config.

mod_rewrite can be complicated, but it doesn't need to be. Don't be scared, and don't try to find an exotic workaround which will end up more complicated than modifying a few examples of RewriteRule found online. :)

Upvotes: 0

anubhava
anubhava

Reputation: 786021

A simple Javascript will be sufficient for this:

if ((n=location.href.indexOf("/index.php")) > -1)
   location.assign(location.href.substring(0, n));

Upvotes: 1

Related Questions