In the way to remove /index.php (avoid duplicate content) almost there

Started by distante, February 03, 2012, 09:07:47 PM

Previous topic - Next topic

distante

So! this is the thing.

I'm trying to make the /index.php disappear, I know there's a mod in the mod site, but this mod crashes the css files in the first load, anyway, I remove the index.php in the forum menus thanks to nend in this topic

He also give me a hint for a php code to evaluate the request URI in the smf's index.php. So far so good.

I used this code in the very beginning of the index.php file


if (stristr($_SERVER["REQUEST_URI"], '/index.php')) and ( {
header("HTTP/1.0 301 Moved Permanently");
header("Location: http://www.forosperuanos.net/");
exit();
die();
}


It worked fine removing the access to /index.php, but it crashed the attachments and avatars.

Can anyone give me a hint on How can I do what I want to do?



Pd: also facebook takes /index.php and /  as two different pages!!!  :-\

Oldiesmann

You can remove index.php from all but the main forum URL with Pretty URLs, SimpleSEF or a similar mod. There isn't an easy way to completely remove it from the URL otherwise.

Chen Zhen

I'm not exactly clear on what you're trying to accomplish but it sounds like you might be better off using Apache to do this.
You can create a .htaccess file & put it in the main directory where you include conditional rewrite rules.

Google .htaccess & you will see many examples of what I am talking about.
ie. ref. http://corz.org/serv/tricks/htaccess2.php

If you're just trying to change /index.php to / using php, you can use this: (only the index)

global $scripturl;
if ($_SERVER['REQUEST_URL'] == $scripturl)
{@header('Location: '. str_replace('index.php', '', $_SERVER['REQUEST_URL']));}


For all instances of index.php (except admin?):

if (preg_match('/index.php/i', $_SERVER['REQUEST_URI']))
{@header('Location: '. str_replace('index.php', '', $_SERVER['REQUEST_URL']));}




My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

distante

Hello -underdog- thanks for give me a hand,

I tried with the htaccess files, but I wasn't able to do it  because the call to some functions stop working.


A question, Do I have to use that codes in the index.php or in the index.template.php?

Chen Zhen

Quote from: distante on February 04, 2012, 11:36:56 AM
Hello -underdog- thanks for give me a hand,

I tried with the htaccess files, but I wasn't able to do it  because the call to some functions stop working.


A question, Do I have to use that codes in the index.php or in the index.template.php?

I see that you use Simple Portal & that is a smart choice btw.
Create a php portal block (ie. header block), put the code in the block, check mark no body + no title & use the drop-down in the display options by setting it to everywhere. This way you do not have to edit any files.


My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

distante

Thanks -Underdog- !!!

It worked just fine. The only change that I made was to show it only in the portal, because isn't need it in other places, besides (maybe is my imagination but) i feel a significative delay on load the portal with this code.

Do you think that I should add the 301 header also?

Chen Zhen

Quote from: distante on February 04, 2012, 05:34:57 PM
Thanks -Underdog- !!!

It worked just fine. The only change that I made was to show it only in the portal, because isn't need it in other places, besides (maybe is my imagination but) i feel a significative delay on load the portal with this code.

Do you think that I should add the 301 header also?

If you think you're getting a delay due to  the portal being loaded, then do this edit..
File: index.php  (<-- main directory)
Find:

// The main controlling function.
function smf_main()
{
global $modSettings, $settings, $user_info, $board, $topic, $board_info, $maintenance, $sourcedir;



Replace with...

// The main controlling function.
function smf_main()
{
global $modSettings, $settings, $user_info, $board, $topic, $board_info, $maintenance, $sourcedir, $scripturl;

if ($_SERVER['REQUEST_URL'] == $scripturl)
{
@header ('HTTP/1.1 301 Moved Permanently');
@header('Location: '. str_replace('index.php', '', $_SERVER['REQUEST_URL']));
}


Edit -> Apologies to the changes in the above. I was getting an unrelated error that at the time I thought was related.  ???
Only use the $scripturl edit shown above when editing the index (other is ok for block on home page only).

My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

Chen Zhen

So this is satisfactory? If so, please flag the topic as solved.
Thank you.

My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

distante

Sorry!!!

Now my host is in maintenance and I can't edit nothing because is too slow, maybe this was the reason why I feel the root slow too, I will post in a couple of hours when the host finish the maintenance.

But I will post the result !

Chen Zhen

Quote from: distante on February 05, 2012, 05:14:34 PM
Sorry!!!

Now my host is in maintenance and I can't edit nothing because is too slow, maybe this was the reason why I feel the root slow too, I will post in a couple of hours when the host finish the maintenance.

But I will post the result !

No need to be sorry.  ;)

I think you may be correct & it delays an extra 1-2 seconds.
Without attempting to use an .htaccess file I'm not sure you can get around that for some browsers.
I attempted 2 other methods with the same result.

Assuming you keep $scripturl in the global:

if ($_SERVER['REQUEST_URL'] == $scripturl)
{
$location = str_replace('index.php', '', $_SERVER['REQUEST_URL']);
@header ('HTTP/1.1 301 Moved Permanently');
@header("Location: " . $location, 0);
}


or


if ($_SERVER['REQUEST_URL'] == $scripturl)
{
@header ('HTTP/1.1 301 Moved Permanently');
@header("Location: /", 0);
}


.. wth Chrome there doesn't seem to be any delay. With FF or Opera I get 1-2 second delay. Seems to be the general behavior even if you set the time to 0 which is  default anyway.


My SMF Mods & Plug-Ins

WebDev

SMF support staff should be shaping a positive community experience & not provoking an argument or emotional reaction.

distante

Ready! The code worked perfect! thanks a lot -underdog-!


I used this one


if ($_SERVER['REQUEST_URL'] == $scripturl)
{
@header ('HTTP/1.1 301 Moved Permanently');
@header("Location: /", 0);
}


Tested in ; IE8, FF, OPERA, and Safari.
:D [TOPIC SOLVED] :D



Pd: I think you should post this as a tip/trick in the forum ;)

Advertisement: