Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 4th Apr 2015, 12:58 PM   #1
Warrior Member
Registered Member
 
Join Date: 2013
Posts: 12
Thanks: 5
Thanked 0 Times in 0 Posts
Default
.htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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.
KbPl is offline  
Unread 4th Apr 2015, 02:32 PM   #2
HyperActive Warrior
War Room Member
 
emptee's Avatar
 
Join Date: 2014
Location: New Zealand
Posts: 210
Thanks: 25
Thanked 81 Times in 71 Posts
Default
Re: .htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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

emptee is offline  
The Following User Says Thank You to emptee For This Useful Post:
Unread 4th Apr 2015, 03:56 PM   #3
Warrior Member
Registered Member
 
Join Date: 2013
Posts: 12
Thanks: 5
Thanked 0 Times in 0 Posts
Default
Re: .htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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
KbPl is offline  
Unread 4th Apr 2015, 08:53 PM   #4
Bananas are Yellow!
Registered Member
War Room Member
 
David Beroff's Avatar
 
Join Date: 2013
Location: Wyoming, PA, USA
Posts: 1,046
Thanks: 2,549
Thanked 1,600 Times in 928 Posts
Blog Entries: 4
Default
Re: .htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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.

Put MY voice on YOUR video: AwesomeAmericanAudio.com
David Beroff is offline  
The Following User Says Thank You to David Beroff For This Useful Post:
Unread 4th Apr 2015, 10:58 PM   #5
HyperActive Warrior
War Room Member
 
emptee's Avatar
 
Join Date: 2014
Location: New Zealand
Posts: 210
Thanks: 25
Thanked 81 Times in 71 Posts
Default
Re: .htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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

emptee is offline  
The Following User Says Thank You to emptee For This Useful Post:
Unread 5th Apr 2015, 08:16 AM   #6
Warrior Member
Registered Member
 
Join Date: 2013
Posts: 12
Thanks: 5
Thanked 0 Times in 0 Posts
Default
Re: .htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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
KbPl is offline  
Unread 5th Apr 2015, 05:11 PM   #7
HyperActive Warrior
War Room Member
 
emptee's Avatar
 
Join Date: 2014
Location: New Zealand
Posts: 210
Thanks: 25
Thanked 81 Times in 71 Posts
Default
Re: .htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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

emptee is offline  
Unread 8th Apr 2015, 03:20 AM   #8
Active Warrior
 
Leon William's Avatar
 
Join Date: 2014
Location: Bangalore
Posts: 53
Thanks: 0
Thanked 3 Times in 3 Posts
Default
Re: .htaccess 301 redirect subdomain
Share on: 
fb share twitter share gplus share more share

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
Leon William is offline  
Closed Thread


Bookmarks

Tags
301, htaccess, redirect, subdomain


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 09:48 PM.