Mobile app version of vmapp.org
Login or Join
Megan663

: How to do 301 redirect for complete URL? 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

@Megan663

Posted in: #301Redirect #Htaccess #ModRewrite

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:
m.somesite.com.au/site/somesite/faqs -> 301 www.somesite.com.au/faqs/

And of course redirect global subdomain in this way:
m.somesite.com.au -> 301 www.somesite.com.au

what actually works. What am I doing wrong?

For example:

This redirects work

redirect 301 /about-us/go-social/ www.somesite.com/about-us/ redirect 301 /about-us/team/John/ 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 m.somesite.com.au/site/somesite/faqs 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/?$ www.somesite.com.au/faqs/


# Global subdomain redirect
RewriteCond %{HTTP_HOST} ^m.somesite.com.au [NC]
RewriteRule ^(.*) www.somesite.com.au/ [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/ www.somesite.com.au/about-us/ redirect 301 /about-us/rider-profiles/Alex/ www.somesite.com.au/about-us/ {...}

# redirect m.somesite.com.au/site/somesite/faqs to 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/?$ 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 ^(.*) www.somesite.com.au/ [R=301]

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Megan663

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

redirect 301 m.somesite.com/site/somesite/faqs 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 www.somesite.com/faqs

UPDATE: 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. (?)

UPDATE: Since m.example.com and 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




UPDATE: (After posting .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 ^(.*) www.somesite.com.au/ [R=301]


Slight tidy...

RewriteCond %{HTTP_HOST} =m.somesite.com.au
RewriteRule (.*) www.somesite.com.au/ [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme