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

How to do 301 redirect for complete URL?

· 52CE 我爱测

I have a lot of URL, which can't be redirected, or, better, rewrited with mod_rewrite in .htaccess, so instead rewriting, I have created 301 redirects manually. It isn't too much records, about 40 redirects, and I am basically co…

问题原文

原帖链接 · Alan Kis · CC BY-SA 3.0

I have a lot of URL, which can't be redirected, or, better, rewrited with mod_rewrite in .htaccess, so instead rewriting, I have created 301 redirects manually. It isn't too much records, about 40 redirects, and I am basically copying URL from Excel table. But how to redirect complete URL on subdomain to main domain:

I want to redirect in this way:

http://m.somesite.com.au/site/somesite/faqs -> 301 http://www.somesite.com.au/faqs/

And of course redirect global subdomain in this way:

http://m.somesite.com.au -> 301 http://www.somesite.com.au

what actually works. What am I doing wrong?

For example:

This redirects work

redirect 301 /about-us/go-social/ http://www.somesite.com/about-us/
redirect 301 /about-us/team/John/ http://www.somesite.com/about-us/

And whole bunch of similar redirects, and then about then redirects like this, which don't work:

This redirects don't work

redirect 301 http://m.somesite.com.au/site/somesite/faqs http://www.somesite.com.au/faqs/

Can I redirect complete URL in that way, or is there another solution? Still this isn't working, using RewriteCond:

# Single URL 

RewriteCond %{HTTP_HOST} ^m\.somesite\.com\.au$ [NC]
RewriteRule ^site/somesite/faqs/?$ http://www.somesite.com.au/faqs/ 


# Global subdomain redirect
RewriteCond %{HTTP_HOST} ^m\.somesite\.com\.au  [NC]
RewriteRule ^(.*) http://www.somesite.com.au/$1 [NC,L,R=301]

.htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


# 301 Redirects
redirect 301 /about-us/go-social/ http://www.somesite.com.au/about-us/
redirect 301 /about-us/rider-profiles/Alex/ http://www.somesite.com.au/about-us/
{...}

# redirect http://m.somesite.com.au/site/somesite/faqs to http://www.somesite.com.au/faqs/
# m.somesite.com.au redirects
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^m\.somesite\.com.au$ [NC]
RewriteRule ^site/somesite/faqs/?$ http://www.somesite.com.au/faqs/ [NC,L,R=301]


# Global subdomain redirect
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^m\.somesite\.com.au  [NC]
RewriteRule ^(.*) http://www.somesite.com.au/$1 [R=301]

已采纳解答

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

redirect 301 http://m.somesite.com/site/somesite/faqs http://www.somesite.com/faqs/

This doesn't work because the source URL needs to be a URL-path, starting with a slash (as you have used for the redirects that work), not an absolute URL.

In other words, it should be written as the following (in a .htaccess file located at the subdomains document root):

redirect 301 /site/somesite/faqs http://www.somesite.com/faqs

Note that I have removed the trailing slash from the end of the target URL. Either include the trailing slash for both or omit it for both.

Can I redirect complete URL in that way

What do you mean by "complete URL"? This is the same as your first redirects. You can redirect the complete URL if you want. (?)

Since m.example.com and www.example.com are pointing to exactly the same place (as stated in comments) you need to be careful of redirect loops, although you should be OK with this one. However, this may be better done with mod_rewrite (see below) since you can then specifically check for the .host.

Still this isn't working, using RewriteCond

Note that Redirect (mod_alias) and RewriteCond / RewriteRule (mod_rewrite) belong to different Apache modules. So, you should never mix external redirects from both modules, since the order of execution can be unexpected.

I assume you are doing this already, but... in order to use mod_rewrite you need to enable the rewrite engine and ensure that FollowSymLinks is also enabled:

Options +FollowSymLinks
RewriteEngine On

(After seeing your complete .htaccess file...) The order of directives is important. These external redirects (using mod_rewrite) must come before the WordPress internal rewrites.

External redirects should come at the very top of your .htaccess file.

You only need to include the RewriteEngine and Options directives once at the very top of your file.

Remove/convert the mod_alias Redirects, since they might conflict with the mod_rewrite redirects.

RewriteCond %{HTTP_HOST} ^m\.somesite\.com.au  [NC]
RewriteRule ^(.*) http://www.somesite.com.au/$1 [R=301]

Slight tidy...

RewriteCond %{HTTP_HOST} =m.somesite.com.au
RewriteRule (.*) http://www.somesite.com.au/$1 [R,L]

Either escape all your literal dots (\.) and use start (^) and end ($) anchors, or use an exact match (= prefix / lexicographically equal operator) as I have done. You don't need anchors with a .* pattern. Good practise to use the L flag with redirects (even though this is the last one).

Always test with temporary (R or R=302) redirects - these are not cached by the browser, unlike permanent redirects. When you are happy it is working then change to a permanent (301) redirect.

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

资料来源与适用范围

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

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

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