.htaccess 301 redirect subdomain

by KbPl
7 replies
Hello.

I wish to change a category name on my wordpress site. That site is on a subdomain.

I wish to change from (eg):

sub.example.com/old/

to

sub.example.com/new/

I noticed that there is no .htaccess file on the subdomain folder, so I guess I'll have to add some code to the "main domain" .htaccess, right?

And may you please tell me wich code should I add?

I've search about this question, but I didn't find anything about this particular case (subdomains).

Thank you for your time.
#301 #htaccess #redirect #subdomain
  • Profile picture of the author emptee
    Hi KbPI,

    This should do the trick, if I understand you correctly

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^sub.example.com [NC]
    RewriteRule /old/ http://sub.example.com/new/ [L,R=301]

    Pretty basic redirect - the rule essentially goes like this.. "when requested for /old/, redirect to /new/, but only if HTTP_HOST (the requested host) starts with sub.example.com"

    Cheers,
    Michael
    {{ DiscussionBoard.errors[9983221].message }}
  • Profile picture of the author KbPl
    Hello emptee!

    Thank you very much for you help

    Cheers!

    Edit:

    Oops. I have a question, please...

    Should I add that code to .htaccess from the "main domain folder" (example.com) or should I create a new .htaccess file inside the sub.example.com folder?

    Thank you again
    {{ DiscussionBoard.errors[9983334].message }}
    • Profile picture of the author David Beroff
      Originally Posted by KbPl View Post

      Should I add that code to .htaccess from the "main domain folder" (example.com) or should I create a new .htaccess file inside the sub.example.com folder?
      They could be hosted on different physical servers, so it's safest to create a new one in the subdomain's root.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[9983664].message }}
    • Profile picture of the author Leon William
      Originally Posted by KbPl View Post

      Hello emptee!

      Thank you very much for you help

      Cheers!

      Edit:

      Oops. I have a question, please...

      Should I add that code to .htaccess from the "main domain folder" (example.com) or should I create a new .htaccess file inside the sub.example.com folder?

      Thank you again

      Hi,

      No need to add new .htaccess file as it is already present in the sub-domain Wordpress folder which you have installed.

      Note : 1) Login to your Cpanel
      2) Click on File manager option
      3)Click on the option(Show hidden files) showed in pop up dialogue box and proceed. You will find .htaccess file in that
      {{ DiscussionBoard.errors[9989858].message }}
  • Profile picture of the author emptee
    The .htaccess file that needs to be modified.. is whichever one lives in the correct vhost

    Eg. if you have two vhosts set up, say...

    domain.com -> /home/apache/domain.com/public_html
    sub.domain.com -> /home/apache/sub.domain.com/public_html

    Then the .htaccess file you'll want to modify would be /home/apache/sub.domain.com/public_html/.htaccess

    But.. if you're serving both sub.domain.com and domain.com from the same vhost, then whichever .htaccess file is in the root of that vhost is the right one.

    How do you know which configuration you're running if you don't manage the vhosts yourself? Well - simple - if going to either sub.domain.com or domain.com results in the same website being shown, then you most likely only have one vhost configured for both domains.. otherwise.. you've probably got seperate vhosts.

    At the end of the day - it doesn't really matter if you put it into the wrong .htaccess file, as it will have no effect at all if it's in the wrong vhost, due to the "but only if HTTP_HOST (the requested host) starts with sub.example.com" condition.

    Cheers,
    Michael
    {{ DiscussionBoard.errors[9983790].message }}
  • Profile picture of the author KbPl
    Thank you emptee!

    you have been very helpful


    Cheers!

    Edit:

    It's not working

    Well, there was a .htaccess file inside sub.domain folder.

    Here's what .htaccess looks like before adding that code:

    # Use PHP5.4 as default
    AddHandler application/x-httpd-php54 .php

    # 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

    Does it matters where put the code you gave me? I've tried on almost every line. Top, bottom, middle.

    Any ideas, please?
    Thank you again
    {{ DiscussionBoard.errors[9984224].message }}
  • Profile picture of the author emptee
    Ahh - you're using wordpress!

    I'd just use a plugin if it were me - there are a ton of redirection plugins that will do what you need..

    Alternatively, the code would have to go just after the RewriteBase line.. and it may be overwritten if a plugin rewrites your .htaccess file (Yoast does this, from memory...)

    Or.. you could put the code at the bottom of the file and make sure /old/ exists within the vhost.

    Do yourself a favor and use a plugin - it'll be easier to manage in the future.. and unless you're trying to squeeze the last few visitors out of your server, the performance impact will be negligible.


    Cheers,
    Michael
    {{ DiscussionBoard.errors[9984988].message }}

Trending Topics