Forums

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

Home Forums Other .htaccess rewrite url

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #45792
    cssmann
    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?

    I tried something like this:


    RewriteEngine On
    RewriteBase /index/

    RewriteCond %{REQUEST_FILENAME} !=f
    RewriteCond %{REQUEST_FILENAME} !=d

    RewriteRule ^(.*)$ index.php/$1

    but it didn’t work, someone who know how to do? :-)

    #140089
    chrisburton
    Participant
    #140101
    cssmann
    Participant

    Thanks this was a very good help, and i have now found a solution ;-)

    #140328
    AWKM
    Participant

    AddType text/x-component .htc
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^GET (.*).php HTTP
    RewriteRule (.*).php$ $1 [R=301]
    RewriteRule (.*)/index$ $1/ [R=301]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.*)/ $1 [R=301]
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1.php [L]

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

    #140363
    cssmann
    Participant

    All i want is the address bar to show the http://example.com/index so people can’t see the file type ;-)

    #140365
    cssmann
    Participant

    When i type in what AWKM said it shows page error :/

    #140367
    __
    Participant

    >All i want is the address bar to show the http://example.com/index so people can’t see the file type ;-)

    …then all you need to do is write your links like

    link!

    and not like

    link!

    and use the .htaccess snippet @ChrisBurton link to. “`example.com/pretty`” is what the user will see in their address bar when they click on it; “`example.com/pretty.php`” is the page Apache will serve.

    *****
    >When i type in what AWKM said it shows page error :/

    right – as I said, it’s a touchy/inefficient “solution” to a problem that doesn’t exist. This is nothing against @AWKM; it’s just not a good idea in the first place.

    (also, it shouldn’t be all on one line like that: it should look like this in your `.htaccess` file:

    RewriteEngine On
    RewriteBase /

    RewriteCond %{THE_REQUEST} ^GET (.).php HTTP
    RewriteRule (.).php$ $1 [R=301]
    RewriteRule (.)/index$ $1/ [R=301]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /$
    RewriteRule (.)/ $1 [R=301]

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

    #140379
    cssmann
    Participant

    THANKS, it works something like perfekt :-O ;-)

    #140381
    cssmann
    Participant

    can you tell me the difference between this to codes?

    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^([^.]+)$ $1.php [NC,L]

    because this seems to work in the same way, but there isn’t as much coding? :-D

    #140382
    AWKM
    Participant

    Not sure what happened to the formatting.

    I like the solution I use because it works with a variety of link structures.

    If you have /whatever.php it becomes /whatever

    If you have /whatever/ it’ll still work if no actual directory is found

    Deals nicely with redirects

    #140385
    cssmann
    Participant

    yes, i also want it to make the slash after the index, and all the other. But it’s not working for me :/ because if i use what you send me there becomes error, are the something i should change? :D

    #140387
    Alen
    Participant

    You can’t add `/` at the end because that’s requesting `localhost/index/index` hence the error.

    #140431
    __
    Participant

    >Not sure what happened to the formatting.

    indent text four spaces to have it parsed as a code block. e.g.,

        this

    becomes

    this

    read more about [Markdown](http://daringfireball.net/projects/markdown/syntax)

    #140432
    Alen
    Participant

    You can also highlight the code and click the “Code” link above the comment form.

Viewing 15 posts - 1 through 15 (of 17 total)
  • The forum ‘Other’ is closed to new topics and replies.