Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other .htaccess rewrite url Re: .htaccess rewrite url

#140344
__
Participant

>I want .htaccess to rewrite https://www.example.com/index.php to https://www.example.com/index/ in the url bar, how can i do this?

If that’s _literally_ what you want to do, I would recommend reconsidering. To clarify,

**1**) if I **type** this into my address bar:
`http://example.com/index.php`

…are you saying you want _the address I typed, that I can see in the address bar_ to change into
`http://example.com/index`

?

or, **2**) do you mean that, if I type

`http://example.com/index`

…you want to show me the page at `index.php`, but still let me see the “pretty” url I typed?

The latter is what the snippet @ChrisBurton linked to does. It is also a very reasonable and common practice. *

*the title is a little misleading, however: it does not “remove” anything from your URLs. What’s really happening is you leave the file extension off on purpose, and then that .htaccess snippet puts it back on for you.

The _former_, however, is a foolish endeavor – mod_rewrite does **not** have access to your visitor’s address bar (nor should it). In order to change what appears there, you actually have to instruct the browser to make a second request (using the “pretty” url), and then (because the “pretty” url is not the _correct_ url), you have to rewite it _again_ to make use of it.

This is a huge waste of time. You’re turning a single request into two (three, in some cases in @AWKM’s code), all to fix a problem that didn’t exist until you created it. Also, if you’re not very careful, you’ll get yourself stuck in a rewriting loop and your visitor won’t get any page at all.