Forum Moderators: phranque

Message Too Old, No Replies

Redirecting from index.php to index.html

         

ollyno1uk

1:20 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



Hi

I have recently been working on renovating a site. I have decided to use html extensions and the previous site used php

I am now to the point where I want to switch over the home page to the new home page via redirection as I don't want to lose anything within the search engines.

The issue is that I either get a redirection loop or the original index.php.

here is what I have in my htaccess - this is mainly allowing me to ue php within html extensions and keeping the url in the form I like (www.example.com)


AddType application/x-httpd-php htm html php
AddHandler application/x-httpd-php .htm .html

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.co.uk [NC]
RewriteRule ^(.*) http://www.example.co.uk/$1 [R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/ [NC]
RewriteRule ^(.*)index.php?$ http://www.example.co.uk/$1 [R=301,L]

Redirect 301 /operation.php http://www.example.co.uk/

Can someone point me in the right direction of firstly being able to get the index.html to be the default page and how I can then redirect the old.

Thanks

g1smd

8:20 pm on Mar 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your code at the moment has the non-www redirect before the index redirect. That creates a redirection chain for a non-www index request: two redirects one after the other - and that is often a bad thing. The order of the redirects needs to be swapped.

The index redirect code you have is inefficient - there are better examples posted in many recent threads. In particular the .* part is very slow in use.

Additionally, never use Redirect in the same .htaccess file as you have used RewriteRule - use RewriteRule for all of the rules. For that one specific page you are redirecting, list that redirect first before all of the others, and ensure it has both the domain name and [R=301,L] within the rule.

As for your original description of what you want to do... Don't do it!

Redirect both '/index.php' and '/index.htm(l)' to '/' for root and again for all folders. That's a simple change to the code that you already have.

For root use '/' and for folders use '/folder/' in the links within your site. It is links that define URLs. Don't use the index file filename in the links on your site.

Do use the DirectoryIndex directive to specify the name of the file that will serve the content when a URL ending with a / is requested. When you swap the name there, from index.html to index.php, nothing needs to change in the URL.

Make sure that the redirects are listed in most-specific (single page) to least specific (non-www to www) order, and that they all have the full domain name and [R=301,L] within.

ollyno1uk

8:24 am on Mar 18, 2009 (gmt 0)

10+ Year Member



Thanks a lot for taking the time to be so detailed.

I will look for an alternate to the index as you suggested. I guess that I then do exactly the same for index.html to redirect to the route.

With regards to your section about not using redirect - if I wanted o make this rewriterule would it be something like this?

Redirect 301 /operation.php http://www.example.co.uk/

to be replaced with

RewriteRule /operation.php http://www.example.co.uk[R=301]

or is it more complex than this?

jdMorgan

12:03 pm on Mar 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule uses regular-expressions, whereas Redirect uses simple prefix-matching. So the syntax differs.

Also, a spaces is required before the flags, and you should always use the [L] flag on every rule unless you know the reason why you can't use it.


RewriteRule ^operation\.php$ http://www.example.co.uk [R=301,L]

Jim

g1smd

8:26 pm on Mar 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I realise my wording was slightly unclear. It should have said redirect /index.html and /index.php to / and redirect /folder/index.html and /folder/index.php to /folder/ making sure that the redirect contains the full domain name, preserves the folder path, and ends with [R=301,L]. That will do what you need to do.