Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

Redirect Based On URL Used To Access Site

         

Planet13

8:03 pm on Nov 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey everyone:

Would like to redirect users via 301 if they use the shared server URL instead of the domain name.

Let me explain:

The site is on a shared server. It is available via BOTH the our_domain_name.com AND via the server_name.us (in this case, it is like s12345.shared_server.us

I want to make it so that if someone goes to

s12345.shared_server.us

it 301 redirects to

our_domain_name.com

Is there a way to do this in WP? Namely, is it possible to see if the URL contains "s12345.shared_server.us" and have it redirect via 301 to our_domain_name.com?

Thanks in advance.

~~~~~

BTW: I have canonical tags set up so that even if they are served the s12345.shared_server.us they get the canonical tag for our_domain_name.com

lucy24

9:32 pm on Nov 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there a way to do this in WP?

Probably-- but a redirect will use far fewer server resources if you code it yourself. (The same goes for 404/410.)

A domain-name-canonicalization redirect is always set up as a negative: "If the request is for anything other than my preferred form of the name, issue a redirect." It is normally your very last external redirect, meaning that it should go immediately before the WordPress section of your config or htaccess file. In Apache it looks like this:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


Wording is slightly different if the rule is lying loose in the config file, not in a <Directory> section or htaccess. The ()? part is for requests from certain elderly user-agents that don't send a hostname at all. (Do these still even exist?)

What happens to requests for other sites that live on the same server?

Planet13

10:18 pm on Nov 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you, Lucy.

So in my case, should I use:

RewriteCond %{HTTP_HOST} !^(s12345\.shared_server\.us)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

And will the above also work if they request something like:

s12345.shared_server.us/about/

s12345.shared_server.us/contact/

s12345.shared_server.us/some-other-page/

Also, I see that the following already exists in the .htaccess file in the WP directory:


# 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


Actually, that is the ONLY code that exists in the .htaccess file. Just kindly let me know if I need to change anything around.

Thanks in advance.

[edited by: phranque at 2:44 am (utc) on Nov 6, 2014]
[edit reason] exemplified domain [/edit]

lucy24

11:43 pm on Nov 5, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



not2easy can explain the WP package more elegantly. Short version: Leave it alone for now. Any additions that you yourself make to RewriteRules in htaccess should go before the existing WP block.

This only becomes important when you change your WP installation, because this will be the part of htaccess that potentially gets overwritten. If WP can't find its own labeled section, it may just replace your entire htaccess. Apparently early versions of WP always behaved this way, so it was next to impossible to maintain your own htaccess if you had a WP site. Conversely, if you've put other stuff inside the WP section (the part delimited by # comment lines) they may get overwritten. You don't want that to happen either.

Look in the last few days of posts next door in the Apache subforum. There's a longish thread started by someone who is also on a WP installation, so you can swipe any useful advice that he received over there.

So in my case, should I use:
RewriteCond %{HTTP_HOST} !^(s12345\.shared_server\.us)?$

No, no, other way around. In the condition, name the correct, preferred form of your hostname. This will be identical to the hostname given in the target-- that's the whole point of the rule. The condition says "If the requested hostname is not etcetera".

Planet13

12:59 am on Nov 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks so much, Lucy! Works fine (placed the code above the wordpress stuff, as you suggested).

Thanks again.