Removing index.php from url

Francis Daly francis at daoine.org
Mon Dec 3 21:11:29 UTC 2012


On Sun, Dec 02, 2012 at 07:08:55PM -0500, adambenayoun wrote:

Hi there,

I don't have a direct answer to your questions, but there is something
I'd like to comment on...

> So while http://www.domain.com/contact works -
> http://www.domain.com/index.php/contact will fetch the same content aswell
> and I'd like to get rid of it for SEO purpose.

"get rid of" means: if a request comes in for /index.php/anything,
it should be permanently redirected to /anything.

> right now there's a if statement that takes care
> of removing the index.php however I know that if is evil and therefore
> shouldn't be used.

Here's the thing: why do you believe that? It seems to be a reasonably
common belief.

The wiki page about it, http://wiki.nginx.org/IfIsEvil, has as its first
line: "Directive if has problems when used in location context". You're
not using it in location context, so you should be fine. Outside of
location context, I don't think it counts as any more evil than any of
the rewrite module directives.

Anyway: on to the questions...

> On top of that the only instance that won't work well is
> http://www.domain.com/index.php which in this case instead of redirecting to
> www.domain.com will just display a blank page with a 301 status code.

So: what do you want to happen when someone requests /index.php without
an immediately-following / ? Just process the page, or redirect to / ?


http://nginx.org/r/location for the details, but you can use "location
= /" or "location = /index.php" to handle the "exact" case (which can
involve fastcgi_pass and the like); then "location ^~ /index.php/" for
everything that should be redirected to "no index.php" (possibly with
a map (http://nginx.org/r/map) to find the thing to redirect to); and
then the rest stays as it is.


> On top of that - if you guys spot any problem with my config - I'd love to
> hear what I am doing wrong and what can be improved.

> error_page 403 404 http://www.domain.com/notfound;

That bit possibly does not do what you want.

> if ( $request_uri ~ "^/index.php" ) {
> rewrite ^/index.php(.*) $1 permanent;
> }

With the pure-location setup, that bit becomes unnecessary.

> location /min {
> try_files $uri $uri/ /min/index.php?q=;

No $args there.

> location /blog {
> try_files $uri $uri/ /blog/index.php?q=$1;

It's not obvious what $1 is set to there.

> location /apc {
> try_files $uri $uri/ /apc.php$args;

No ? there.

Each of those may or may not do what you want. If they work, that's fine.

> location ~ \.php {

> location ~* ^.+\.(ht|svn|git)$ {

> location ~*

An occasional suggestion on the list is to avoid top-level regex match
locations, for ease of reading/scalability reasons.

If you choose to follow that suggestion, those would need to be rewritten.

It may not be worth you following that suggestion, depending on how your
files are organised.

Cheers,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list