Welcome to WebmasterWorld Guest from 54.224.5.186

Forum Moderators: Ocean10000 & incrediBILL & phranque

Redirecting index.php within a directory

     
5:39 pm on May 2, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Aug 16, 2004
posts: 354
votes: 1


I currently have a rule in htaccess to redirect example.com/index.htm to example.com/

I now just need to add a rule to redirect the index.php of a specific directory to example.com/directory/

Would this be ok to include in my htaccess file:

RewriteRule ^(.*)index\.(php|htm?)$ /$1 [R=301,NC,L]


Or should I keep the two rules separate (one for index.htm of main home page and other for index.php of /directory/ like the below:
RewriteCond %{THE_REQUEST} ^.*/index.htm
RewriteRule ^(.*)index.htm$ http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)/directory/index.php$ http://www.example.com/$1 [R=301,L]
6:32 pm on May 2, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13674
votes: 439


Is index.php your actual index file as set in the DirectoryIndex directive? If so, you don't need conditions at all: just append the [NS] flag to your RewriteRule.

Now, although you don't technically need a condition, you may choose to use one for a different reason: to save the server the trouble of capturing on every single request ever, when that capture is just going to be thrown away. Like this, for example:
RewriteCond %{REQUEST_URI} ^/((?:\w+/)*)index\.php
RewriteRule index\.php$ http://example.com/%1 [R=301,NS,L]

The "index.htm" issue came up in the adjoining thread (which I think I had better bow out of, because you're getting contradictory advice already). Yes, you need to maintain a rigorous distinction between your actual index file names, and others that may simply be requested. Check your logs; I really doubt you need a specific .htm rule, though you can choose to include it in the .html rule. This you do need, because Certain Search Engines will request "index.html" just to see if it resolves, even if it has never been used in visible URLs.

Why just one directory? If it's always "index.php" then you'll need to redirect everywhere. Or do you have a mix of index.html and index.php in different directories? Eeuw.
6:51 pm on May 2, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Aug 16, 2004
posts: 354
votes: 1


Thanks, Lucy - you've been a massive help to me recently.

Basically, I use html pages on 90% of the site (this is where the redirect for index.htm comes in.

However, on the directory we were discussing in the other thread, those pages will be written in .php (as a reminder from the other threads, I'm replacing WordPress with standalone php files in their currently location so nothing changes in the eyes of a search engine ) so need the /exampledirectory/index.php file to redirect to /exampledirectory/ as it does in WordPress.

Thanks again. I would have been lost without your advice over the past few days. Hopefully, all my questions relating to these subjects will help other people here.
12:07 am on May 3, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13674
votes: 439


Both html (with htm extension) and php on the same site, hmm. Maybe this, as a single rule

RewriteCond %{REQUEST_URI} ^((\w+/)*)index\.(php|htm)
RewriteRule index\.(php|htm) http://www.example.com/%1 [R=301,L,NS]

That's assuming the various index.htm and index.php are set by DirectoryIndex directives. You can either make a supplementary htaccess containing only a DirectoryIndex line for those few directories that need to be different, or put multiple options in the same line, like
DirectoryIndex index.htm index.php
listing them in order of overall frequency. The server will look for one after another, stopping as soon as it finds one.

You should still have a separate redirect for "index.html" but if you're getting tired, you can postpone this until the day you actually see the Googlebot asking for wholly imaginary "index.html" files.

Edit: Wherever I say \w+ you may need to replace it with [\w-]+ or [\w.]+ or whatever applies in your specific site. A hyphen - doesn't count as a "word" character, though a lowline _ does.
8:15 am on May 3, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Aug 16, 2004
posts: 354
votes: 1


Cheers, Lucy.

For simplicity (on my part). Is there anything "wrong" with me keeping these two rules


RewriteCond %{THE_REQUEST} ^.*/index.htm
RewriteRule ^(.*)index.htm$ http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)/directory/index.php$ http://www.example.com/$1 [R=301,L]


If that is OK, do I just need to add "DirectoryIndex index.htm index.php" to the top of the htaccess file?
4:02 pm on May 3, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13674
votes: 439


Presumably you've already got a DirectoryIndex directive somewhere, or your various directories wouldn't work in the first place. If you're on shared hosting, it's possible they have a generic directive along the lines of
DirectoryIndex index.html index.php index.htm
which is inherited right down the line to all directories everywhere.

No, there's nothing wrong with keeping the two rules separate. BUT there is absolutely no reason for
%{THE_REQUEST} ^.*/index.php
Just say
%{THE_REQUEST} index\.php
without opening anchor. Literal periods should be escaped, but here it's a non-lethal error. (That is, you will not see a lot of requests for "indexxphp" or "index-php" or similar.)
4:50 pm on May 3, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Aug 16, 2004
posts: 354
votes: 1


Thanks, Lucy :-)
11:52 am on May 8, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Aug 16, 2004
posts: 354
votes: 1


Just as an update to this...I've managed to get 99% of everything. However, I just have one issue:

Basically:

www.example.com/folder/index.php redirects to www.example.com/folder/index/
However, I want it to redirect to www.example.com/folder/

Is there anything using those rules above that could be missing? Is it even a problem at all if the end page does work and all my internal links, etc aren't changing?

I suppose my main question is: if nobody has the link to www.example.com/folder/index.php or www.example.com/folder/index/, and all my internal links show www.example.com/folder/ (the correct page), and I use canonical tags on all pages, does it even matter?...unless it's a quick fix using htaccess (I've been mucking around for hours but haven't worked it out).
3:10 pm on May 8, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Aug 16, 2004
posts: 354
votes: 1


I think I may have "kinda" solved the problem. What I've done is rename the index in that subfolder from php to htm and it seems to work fine.
9:50 pm on May 8, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13674
votes: 439


www.example.com/folder/index.php redirects to www.example.com/folder/index/
However, I want it to redirect to www.example.com/folder/

Ah, that's your other rule kicking in: the one that redirects pagename.php to pagename/ (different thread, I think). You'll need a RewriteCond on that rule that says something like
RewriteCond %{REQUEST_URI} !index\.php
or, wait, you don't need that, just add a [NS] flag to your php redirect.
10:02 pm on May 8, 2017 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:Aug 16, 2004
posts: 354
votes: 1


Thanks, Lucy (again). Will give that a try.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members