- This topic is empty.
-
AuthorPosts
-
June 24, 2013 at 9:22 am #45792
cssmann
ParticipantI 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} !=dRewriteRule ^(.*)$ index.php/$1
but it didn’t work, someone who know how to do? :-)
June 24, 2013 at 10:37 am #140089chrisburton
ParticipantJune 24, 2013 at 12:00 pm #140101cssmann
ParticipantThanks this was a very good help, and i have now found a solution ;-)
June 25, 2013 at 2:06 pm #140328AWKM
ParticipantAddType 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]June 25, 2013 at 2:56 pm #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.
June 25, 2013 at 3:26 pm #140363cssmann
ParticipantAll i want is the address bar to show the http://example.com/index so people can’t see the file type ;-)
June 25, 2013 at 3:32 pm #140365cssmann
ParticipantWhen i type in what AWKM said it shows page error :/
June 25, 2013 at 3:51 pm #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
and not like
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]June 25, 2013 at 4:34 pm #140379cssmann
ParticipantTHANKS, it works something like perfekt :-O ;-)
June 25, 2013 at 4:42 pm #140381cssmann
Participantcan 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
June 25, 2013 at 4:45 pm #140382AWKM
ParticipantNot 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
June 25, 2013 at 4:49 pm #140385cssmann
Participantyes, 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
June 25, 2013 at 5:02 pm #140387Alen
ParticipantYou can’t add `/` at the end because that’s requesting `localhost/index/index` hence the error.
June 25, 2013 at 6:57 pm #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)
June 25, 2013 at 6:58 pm #140432Alen
ParticipantYou can also highlight the code and click the “Code” link above the comment form.
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.