Mobile app version of vmapp.org
Login or Join
Radia820

: Issue with 301 redirect to Index page only 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

@Radia820

Posted in: #301Redirect #Htaccess #Html #Php #Url

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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Radia820

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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 example.com) then 301 redirect to example.com/ (or 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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme