大网数据广告活动
网站运营与故障问答

How to redirect a single web page other than using htaccess

· 52CE 我爱测

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 …

问题原文

原帖链接 · user3636420 · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

...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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2015-02-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。