How to do 301 redirect for complete URL?

13,889

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.

Share:
13,889

Related videos on Youtube

Alan Kis
Author by

Alan Kis

Updated on September 18, 2022

Comments

  • Alan Kis
    Alan Kis over 1 year

    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]
    
    • Admin
      Admin over 8 years
      You seem to be mixing domains (.com, .com.au)? Is it really all the same domain? Just with multiple subdomains? Are the subdomains and main domain hosted at the same place? In a subdirectory? Or completely different host? Where exactly is the .htaccess file?
    • Admin
      Admin over 8 years
      @w3d .htaccess file is located in document root, public_html directory. Subdomain and main domain are hosted on same place. Domain is really same.
    • Admin
      Admin over 8 years
      I've updated my answer with regards to your posted .htaccess file. Note that the order of directives in .htaccess is important and this could be the route cause of the problem here.