2

I'd like to redirect a single web page, it will be a temporary redirect so I don't want it to affect SEO rankings.

So I was thinking of using a method other than htaccess, but if it would be more efficient to use htaccess please let me know.

Also, is it possible to redirect the homepage to another page but have all other pages un-redirected?

So for e.g. www.abc.com redirected to www.example.com

But

www.abc.com/news.html is not affected.

For w3d:

I'm trying to do this:

# Use PHP5.4 as default
RewriteEngine On
RewriteRule ^news1\.html$ https://www.example.com/news1.html [R,L]
RewriteRule ^news2\.html$ https://www.example.com/news2.html [R,L]
RewriteRule ^news3\.html$ https://www.example.com/news3.html [R,L]


Options -Indexes

even tried this:

Redirect 302 /news1.html example.com/news1.html
Redirect 302 /news2.html example.com/news2.html

only the first one gets redirected.

Also do they both execute exactly the same function?

1

2 Answers 2

3

...if it would be more efficient to use htaccess

If you have access to .htaccess then there doesn't seem any reason not to use this. Note that this is a "302 Temporary", not a "301 Permanent" redirect as you had initially tagged in the question.

To redirect just the home page to example.com (an external domain) in .htaccess using mod_rewrite:

RewriteEngine On

RewriteRule ^(index\.html)?$ https://example.com/ [R,L]

If both the source and target domains point to the same place then you'll need to check the requested hostname in a preceding condition. For example:

RewriteCond %{HTTP_HOST} =sourcedomain.example
RewriteRule ^(index\.html)?$ https://example.com/ [R,L]

This redirects when there is no path on the URL (ie. the home page), or index.html only. The R flag on its own indicates a temporary redirect, although you can be explicit and write R=302, but it's the same.

^(index\.html)?$ - This is a regular expression. This is the "pattern" used to match the requested URL. Only if the URL matches the pattern will the redirect occur. ^ and $ are anchors indicating the start and end of the string/URL. ? makes the preceding item optional (in this case the parenthesised sub pattern index\.html is made optional). The . (dot) needs to be backslash escaped (ie. \.) in order to match a literal dot, otherwise it is a special/meta character meaning any character.

Other ways to redirect... JavaScript or META refresh (although that might be perceived as permanent these days I'm not sure - it apparently does pass "link juice" so will affect SEO). The .htaccess method is the preferred method. It is more flexible, the most reliable and arguably less convoluted.

17
  • 1
    Firstly, i will need to redirect only the index.html page. Secondly, what does a 302 redirect do that a 301 doesnt? Commented Feb 15, 2015 at 12:11
  • 1
    I've update the above so that it redirects index.html as well as the empty path. As mentioned, a 302 is a "temporary" redirect. It informs the search engines that it's "temporary". This is the HTTP status code that is returned in the initial response. Whereas a 301 redirect is permanent, the search engines will only index the target URL. The net result to the user is the same, regardless of the "type" of redirect... the user is redirected.
    – MrWhite
    Commented Feb 15, 2015 at 12:15
  • Thanks for that. What does "index\.html"?$ mean? Commented Feb 15, 2015 at 12:37
  • A 302 redirect (being "temporary") should not affect SEO, the search engines should not update their index to the target URL and browsers should not cache it. Whereas a 301 redirect passes all (or most of) the "link juice" to the target URL and will replace the source URL in the SERPs.
    – MrWhite
    Commented Feb 15, 2015 at 12:38
  • 1
    Also, im guessing in this case, it will only pass the "link juice" for the redirected pages, whilst all other pages ranking will remain the same? Commented Feb 15, 2015 at 12:39
2

If you want to redirect a single page, you have options.

The oldest way which may still work, but might be the worst way from SEO point of view is to create an HTML file containing the following:

<html>
<head>
<title>Redirect</title>
<meta http-equiv="REFRESH" content="1;URL=http://www.newsite.com">
</head>
<body>
<!-- add website code here -->
</body>
</html>

The above causes a redirect after 1 second. you can change 1; to 0; after "content" to refresh instantly but that might work with only certain browsers.

A little better way if you have a php enabled server is to create index.php with the following contents:

<?php
header("HTTP/1.1 301 Redirect",true);
header("Location: http://www.newsite.com",true);
?>
<html>
<head>
<title>Redirect</title>
</head>
<body>
Go <a href="http://www.newsite.com">here</a> for the document.
</body>
</html>

You may also follow above posters advice for a redirect, but if you want slightly faster performance, then instead of placing the rules in an .htaccess in the folder, you can add the following to apache's main configuration file (usually httpd.conf) so that apache doesn't have to constantly search many subfolders for the rules:

<directory /path/to/htaccessfile>
#insert rewrite rules here
</directory>

Only thing is that you need high enough access to the server (shell access preferred) and then apache will need to be restarted after this change.

Your Answer

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

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