Welcome Guest, Not a member yet? Register   Sign In
index.php question
#1

[eluser]James Burton[/eluser]
This has really been bothering me today, because I found a solution for this a long time ago but I've since forgotten it. :down:






The issue I have is when people access the main page of my site they can either access it by http://example.com/ or http://example.com/index.php but what I want to do is redirect users who type the second option to the the first.

So people who type or follow a link to http://example.com/index.php will be redirected to http://example.com/

I'm sure there was a way I found in php to check whether 'index.php' was present in the url and if so redirect.
But of course it will be serving the exact same page, I just don't want it to be obvious to the user that the site is powered by php.

If anyone could help me with this it'd solve the bugging feeling I have knowing I have done this before yet can't remember!
:-)
#2

[eluser]theprodigy[/eluser]
Code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.domain.com/$1 [R=301,L]

should do the trick (of course, change domain.com with whatever your domain name is)
#3

[eluser]theprodigy[/eluser]
nevermind, that only works if index.php was the last thing typed in. If it's followed by anything, it won't work.
#4

[eluser]wabu[/eluser]
Hi, does anybody know the answer to this one?

I was going to post the same question and spent a bunch of time reading old posts and web articles but all of the discussion deals with that "other" index.php question.
#5

[eluser]James Burton[/eluser]
I have found the following to work.

Code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ http://example.com/? [R=301,L]

RewriteCond %{query_string} .
RewriteRule (.*) http://example.com/$1? [R=301,L]

It'll remove index.php, index.php? and index.php?any=values&in=url

Additionally the second rule will remove any query strings from any url, so of course this will only work if you're not using query strings for anything on your site, which by default you shouldn't be. Smile

Although I'm no expert in mod_rewrite, so if anyone still knows a better way of doing this please point it out.
:-)
#6

[eluser]wabu[/eluser]
James, thanks for posting that.

I think I won't be able to use it because I do use query strings to, for example, filter an index view with a keyword (a search, in other words), or include a security token.

Neither is unRESTful, in the sense that REST is used in discussions around the interwebs.

I wonder if some regex magic would work?

Anyway it's not the worst thing in the world if somebody types in index.php and gets a (non-404) response, it just seems a bit amateurish.
#7

[eluser]Chad Fulton[/eluser]
[quote author="James Burton" date="1248384350"]I have found the following to work.

Code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ http://example.com/? [R=301,L]

RewriteCond %{query_string} .
RewriteRule (.*) http://example.com/$1? [R=301,L]

It'll remove index.php, index.php? and index.php?any=values&in=url

Additionally the second rule will remove any query strings from any url, so of course this will only work if you're not using query strings for anything on your site, which by default you shouldn't be. Smile

Although I'm no expert in mod_rewrite, so if anyone still knows a better way of doing this please point it out.
:-)[/quote]

IMO this is a very odd solution to your originally stated concern, primarily because since you are using mod_rewrite, the user will still see the index.php part in their browser. This is because mod_rewrite's URL rewriting is purely internal.

Since this is the case, the steps you're taking are not only worthless, but also have harmful effects, since all requests to CodeIgniter must go through index.php. That means that after removing index.php with mod_rewrite (which the end user will not notice), you then have to add it again with mod_rewrite so that CodeIgniter works.

The only possible benefit to this is that you've added a 301 header to the request, which does tell the browser that index.php isn't a location you want. Despite that, you stated that you don't want users to know that you're serving php pages, and 99% of users won't even know what a 301 header is, much less check it.

Here are a two solutions I can think of:

1. Add the following code to the top of your index.php file:
Code:
if(strpos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
    header("HTTP/1.0 404 Not Found");
    die;
}
This will cause all requests with index.php in the URL to result in a standard 404 error, the same as if they went to http://www.example.com/this_file_does_not_exist.php

2. If you want the 404 error to be shown in the same way that CodeIgniter handles them, you could add a precontroller hook that calls a function with this code:
Code:
if(strpos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
    show_404();
}
#8

[eluser]Fielder[/eluser]
This rewrite code only works for Apache correct? What about IIS?
#9

[eluser]James Burton[/eluser]
[quote author="Chad Fulton" date="1248393946"]
Code:
if(strpos($_SERVER['REQUEST_URI'], 'index.php') !== false) {
    header("HTTP/1.0 404 Not Found");
    die;
}
[/quote]

Thanks, that was the actual code I was originally looking for. Smile

Although I'm using output caching on the page, so I believe that stops it from working.

The simple solution I have settled on is renaming the index.php file and setting the new file as DirectoryIndex in .htaccess, that way it'll almost be impossible for the user to type the exact url in.
#10

[eluser]wabu[/eluser]
Thanks for the discussion. I ended up implementing the hook.




Theme © iAndrew 2016 - Forum software by © MyBB