3

I have a bunch of 301 redirects in my .htaccess for old pages, which work great.

But a problem that's been plaguing me for years is that there's a lot of links out there to my old index page (index.html). I want to redirect them to the new index.php, so as to save link juice etc. (if that's still how it works?)

The problem is, if I make a redirect from index.html to index.php it works fine for my main website... but it also affects the other websites (separate URLs) which are running from subfolders on the same server. It makes the index pages of all the other websites redirect to the main one too.

How can I write a redirect that only affects the index page of the main domain, without affecting the other sites? My obvious first thought was to use two absolute URLs, but apparently that's a no-no?

2
  • It would help if you added what you have tried and what your existing redirects are like. For instance, are you using mod_alias Redirect (RedirectMatch) or mod_rewrite RewriteRule?
    – MrWhite
    Jan 1, 2017 at 22:39
  • What I tried before was redirect 301
    – ola.rogula
    Jan 6, 2017 at 5:43

1 Answer 1

0

Assuming your "other websites" are using different domains/hosts then you can check the HTTP_HOST before redirecting. The same as you would do for a canonical www redirect.

However, this does require the use of mod_rewrite. Assuming example.com is your main domain, then you can do something like the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^index\.html$ / [R=301,L]

What this does is... for all requests that match /index.html and the host matches example.com (or www.example.com) then 301 redirect to example.com/ (or www.example.com/).

Note that this simply redirects to / and allows mod_dir to fetch the required DirectoryIndex (ie. index.php). If you really want to be explicit and redirect to index.php (not recommended, unless that is what you are currently doing) then include the full substitution /index.php.

The (www\.)? (making www optional) would be unnecessary if you have a canonical redirect in place.

If you are currently using Redirect (or RedirectMatch), part of mod_alias, then you would ideally convert these to mod_rewrite as well. Otherwise, be careful of any conflicts. mod_alias will run after mod_rewrite, despite the apparent order of the directives in .htaccess.

My obvious first thought was to use two absolute URLs, but apparently that's a no-no?

It's only a "no-no" in so far as the relevant Apache directives don't allow you to specify an absolute URL as the source. They expect a URL-path only. The host part of the source URL is separated out. However, an absolute target URL (or substitution in mod_rewrite terminology) is perfectly OK.

...other websites (separate URLs) which are running from subfolders

Running separate websites in subdirectories off the main domain is not ideal, unless they are closely related. Because .htaccess files are inherited along the filesystem path you can have conflicts.

3
  • 1
    Wow.. thank you for this incredible, detailed and informative answer!!! I'm just trying it... seems to be working!!! =O
    – ola.rogula
    Jan 6, 2017 at 5:46
  • Yes, rerouting to www.example.com is perfect. You're right.. I don't need to redirect to index.php explicitly.. this totally fixes it :)
    – ola.rogula
    Jan 6, 2017 at 5:47
  • Thank you so much! I think I've been plagued by this for like 10 years haha..
    – ola.rogula
    Jan 6, 2017 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.