WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How do you redirect the same slug on 2 different domains in a WordPress Network? (12 posts)

  1. Jonathon Hewitt
    Member
    Posted 2 years ago #

    I have a WordPress network setup as a sub-directory install and everything is working properly accept for a small problem, I need to setup two redirects one for each domain both would be redirected to a sub-domain. Because a WordPress network uses only one single .htaccess file this only seems to work for the two redirects in place for the main network domain but not the other mapped domain.

    The main domain in the network is bchyundai.com and I have mapped the domain name bcmazda.com to the network blog bchyundai.com/bcmazda1/ Now I am having an issue because I need the same slug redirected for each domain (bcmazda.com & bchyundai.com). Here is what I have now that works for half of the goal:

    Redirect 301 /browse/view_detailed/type_used/ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/
    Redirect 301 /browse/view_detailed/type_new/ http://newcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_new/
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCHYUNDAI.com [NC]
    RewriteRule ^(.*)$ http://www.BCHYUNDAI.com/$1 [L,R=301]
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCMAZDA.com [NC]
    RewriteRule ^(.*)$ http://www.BCMAZDA.com/$1 [L,R=301]
    
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
    RewriteRule . index.php [L]

    The goal is for:

    http://www.bchyundai.com/browse/view_detailed/type_used/ to redirect to http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/

    and

    http://www.bcmazda.com/browse/view_detailed/type_used/ to redirect to http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/

    Which thanks to this informational post about WordPress multisite redirects I have gotten closer I think to making this work. But none-the-less, it still doesn't work even using his suggested redirect. Here is what I've tried but doesn't work:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCHYUNDAI.com [NC]
    RewriteRule ^(.*)$ http://www.BCHYUNDAI.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?bchyundai.com\.told [NC]
    RewriteRule ^bchyundai.com/browse/view_detailed/type_used/\.aspx$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCMAZDA.com [NC]
    RewriteRule ^(.*)$ http://www.BCMAZDA.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?bcmazda.com\.told [NC]
    RewriteRule ^bcmazda.com/browse/view_detailed/type_used/\.aspx$ http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
    RewriteRule . index.php [L]

    So the question is, how do you redirect the same slug on two different domains in a single .htaccess file? Is there a way to make two .htaccess files? One for each domain or site on the network?

  2. You would do this:

    RewriteCond %{HTTP_HOST} ^(www\.)?bchyundai.com\.told [NC]
    RewriteRule ^browse/view_detailed/type_used/\.aspx$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/ [R=301,NC,L]

    Don't put ^bcmazda.com/ in there, just a ^ should do.

    Basically you want to say "IF the domain is foo, then do this."

  3. Jonathon Hewitt
    Member
    Posted 2 years ago #

    Thank you for your reply, although I tried this and it did not work. Here is what my code looked like after your suggestion:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCHYUNDAI.com [NC]
    RewriteRule ^(.*)$ http://www.BCHYUNDAI.com/$1 [L,R=301]
    RewriteCond %{HTTP_HOST} ^(www\.)?bchyundai.com\.told [NC]
    RewriteRule ^browse/view_detailed/type_used/\.aspx$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCMAZDA.com [NC]
    RewriteRule ^(.*)$ http://www.BCMAZDA.com/$1 [L,R=301]
    RewriteCond %{HTTP_HOST} ^(www\.)?bcmazda.com\.told [NC]
    RewriteRule ^browse/view_detailed/type_used/\.aspx$ http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
    RewriteRule . index.php [L]

    Thank you again for your help!

  4. I didn't notice this before ... Why do you have \.told in there?

    RewriteCond %{HTTP_HOST} ^(www\.)?bchyundai.com\.told [NC]

    Those should JUST be RewriteCond %{HTTP_HOST} ^www.bchyundai.com [NC]

    (You can lock in the www cause the line BEFORE is saying 'if non-www, send to www.')

  5. Jonathon Hewitt
    Member
    Posted 2 years ago #

    Thank you for finding that, although this still did not work for me here is what I did:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCHYUNDAI.com [NC]
    RewriteRule ^(.*)$ http://www.BCHYUNDAI.com/$1 [L,R=301]
    RewriteCond %{HTTP_HOST} ^www.bchyundai.com [NC]
    RewriteRule ^browse/view_detailed/type_used/\.aspx$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^BCMAZDA.com [NC]
    RewriteRule ^(.*)$ http://www.BCMAZDA.com/$1 [L,R=301]
    RewriteCond %{HTTP_HOST} ^www.bcmazda.com [NC]
    RewriteRule ^browse/view_detailed/type_used/\.aspx$ http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
    RewriteRule  ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
    RewriteRule . index.php [L]

    Now the interesting part is that I think this is somewhat closer to something that could work as the sites didn't throw any kind of error after I uploaded this one, the redirect just didn't go to the subdomain that we need it to go to. This is what I get for not going to school for web dev.

  6. I took Anthropology ;) Didn't have web-dev classes.

    the redirect just didn't go to the subdomain that we need it to go to

    Where DID they go?

  7. Jonathon Hewitt
    Member
    Posted 2 years ago #

    Well by the number of downloads you have you must have learned all this stuff the hard way then, trial and error type of thing. Very impressive! And Anthropology is perhaps the most important thing that one can study, I base the study of all other sciences on that one science!

    I love WordPress and plan to become a recognized WordPress development company in the automotive industry one day. To give you the short synopsis here:

    We are an automotive web design and SEO company and have become resellers of HomeNet Automotive who is a large provider of automotive inventory data for car dealers. They have a "backweb" setup which after a cname record change this allows us to display each dealers inventory on a subdomain of the dealer's domain name. We are one of the first companies to try to fully integrate a dealers inventory using HomeNet into WordPress(to the best of our knowledge).

    If you visit Barnes Crossing Hyundai which is built in WordPress you will notice the inventory search function that we have built that is showing in the header(ours in the header is just a styled version of this: http://usedcars.bchyundai.com/bchyundaiv2/quicksearch-price.asp?type=used which if you click it, it works until rendered using a php iframe), if you just click search you will notice that the redirect in place will bring you to the subdomain (usedcars.bchyundai.com) and that is where our inventory pages are.

    This dealer also has a Mazda dealership named Barnes Crossing Mazda which is a network blog in the same network as the Hyundai site so they are utilizing the same .htaccess file. Firstly, the inventory search function on the Mazda site is just going to the redirect set for Hyundai because it is the same slug (I need same slug to go to diff pages based on which domain they are on). So if you try to use the inventory search function in the header of the Mazda site you will end up on the Hyundai inventory pages due to this.

    If I remove the redirects entirely the inventory search function will input the ending slug, but you end up on a 404 page because it will not go to the subdomain and will still show the www. instead of the intended subdomain usedcars.

    These redirects are basically a hack and the true way to implement this would be using HomeNet's API to build the inventory search and inventory display pages. We are not there yet, we are working on that and then we could scrap the backwebs entirely and display everything locally on pages spooled up by WordPress.

    I could fix the issue for the time being by making each site its own WordPress installation but I really am trying to keep it all in one dashboard using a WP network because I just think this makes sense.

    So to clearly answer your question after somewhat of an explanation behind all this, when I used the code mentioned in my last reply the inventory search function sent me to this url: http://www.bchyundai.com/browse/view_detailed/type_used/ when I really wanted it to send me here: http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/

    (The url it sent me to is the same url that I get sent to when I remove the 301 redirects.)

  8. Trial, error and a LOT of reading.

    Hrm. Let's try removing the \.aspx stuff. You may not need that if the URL is what you say, but let's toss in a wildcard to CYA.

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^bchyundai.com [NC]
    RewriteRule ^(.*)$ http://www.bchyundai.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www.bchyundai.com [NC]
    RewriteRule ^browse/view_detailed/type_used/(.*)$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    RewriteCond %{HTTP_HOST} ^bcmazda.com [NC]
    RewriteRule ^(.*)$ http://www.bcmazda.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www.bcmazda.com [NC]
    RewriteRule ^browse/view_detailed/type_used/(.*)$ http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/ [R=301,NC,L]
  9. Jonathon Hewitt
    Member
    Posted 2 years ago #

    Ok bud sorry for taking a while to update this again and thank you very much as this seems to be even close to perfection. I just have one more question as this is working from the homepage (root domain) but anytime you visit any other page it goes to a 404 page and does not carry over the trailing part of the URL. Example: please visit the Hyundai Sitemap page and then click on the blue search button in the header area (no drop down selection is needed) you will find yourself at the following slug: /sitemap/browse/view_detailed/type_used/ which means that it completely missed the redirect and did not go to the sub-domain either as does now in the root of both sites. If you visit another one of my websites with this same type of search function installed (which is also on a WordPress network) here on the Honda Sitemap page and then you just click "Search Vehicles" in the top right of the sidebar, even though you are on the /sitemap/ slug, the search function seems to trigger the redirect and go to the intended sub-domain and all. I just don't understand why it works on one site and not the other. Uggg... And btw, can I buy you a cup of coffee or a beer for this? lolz this is a big help to my company being able to utilize WordPress networks for future builds.

  10. Actually no. /sitemap/browse/view_detailed/type_used/ is NOT the string we were looking for. We were looking at browse/view_detailed/type_used - No sitemap in there.

    Redirections are VERY tenacious and literal. If you need to redirect a folder, you must be specific about it :)

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^bchyundai.com [NC]
    RewriteRule ^(.*)$ http://www.bchyundai.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www.bchyundai.com [NC]
    RewriteRule ^browse/view_detailed/type_used/(.*)$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    RewriteCond %{HTTP_HOST} ^www.bchyundai.com [NC]
    RewriteRule ^sitemap/browse/view_detailed/type_used/(.*)$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    RewriteCond %{HTTP_HOST} ^bcmazda.com [NC]
    RewriteRule ^(.*)$ http://www.bcmazda.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www.bcmazda.com [NC]
    RewriteRule ^browse/view_detailed/type_used/(.*)$ http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/ [R=301,NC,L]
    
    RewriteCond %{HTTP_HOST} ^www.bcmazda.com [NC]
    RewriteRule ^sitemap/browse/view_detailed/type_used/(.*)$ http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/ [R=301,NC,L]

    (FYI, if someone's wondering why I have to establish the Rewrite Condition before each call, it's because multiple domains on one .htaccess act weirdly - Ditto subdomains. There's probably a more efficient way about it, but this way's never failed me ;) - And there is a paypal link off my domain if you wanna buy me a coffee. Click on my name on the left there.)

  11. Jonathon Hewitt
    Member
    Posted 2 years ago #

    The last question I have is; is there a way to make it a one for all redirect, such as you set one redirect for each domain and all slugs would just carry over?

    This doesn't seem to work in this case. I would like it if even sitemap/browse/view_detailed/type_used/ would still trigger the 1st redirect and carry the visitor over to the intended page: http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/ for example. So basically anything that has "browse/view_detailed/type_used/" in the slug would go there nomatter if it had characters before it or after it. And whats after it would carry over to the end of the intended path. An example of this working is:

    Please visit: PC AutoMall's Sitemap Page (just for an example page with some type of slug) and use the inventory search function that is in the top of sidebar to the right, just click "Search Vehicles" (no drop down selection is needed but will work if you select a make or model) and you will notice that the redirect that is in place there will take you right to the intended page with the "browse/view_detailed/type_used/" at the end.

    This is what I have set up for the PCAutoMall.com .htaccess right now (the one that works nomatter what the slug):

    # BEGIN WordPress
    
    Redirect 301 /browse/view_detailed/type_used/ http://usedcars.pcautomall.com/pcautomallv2/browse/view_detailed/type_used/
    Redirect 301 /browse/view_detailed/type_new/ http://newcars.pcautomall.com/pcautomallv2/browse/view_detailed/type_new/
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+)  wp-includes/ms-files.php?file=$2 [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
    RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
    RewriteRule . index.php [L]
    </IfModule>
    # END WordPress

    So why does this work with PCAutoMall.com but not with BCHyundai.com?

  12. Jonathon Hewitt
    Member
    Posted 2 years ago #

    Ok so I figured it out! I just had to add the little $1 at the end of the destination URLs and it works like a charm!

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^bchyundai.com [NC]
    RewriteRule ^(.*)$ http://www.bchyundai.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www.bchyundai.com [NC]
    RewriteRule browse/view_detailed/type_used/(.*)$ http://usedcars.bchyundai.com/bchyundaiv2/browse/view_detailed/type_used/$1 [R=301,NC,L]
    
    RewriteCond %{HTTP_HOST} ^bcmazda.com [NC]
    RewriteRule ^(.*)$ http://www.bcmazda.com/$1 [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^www.bcmazda.com [NC]
    RewriteRule browse/view_detailed/type_used/(.*)$ http://usedcars.bcmazda.com/bcmazdav2/browse/view_detailed/type_used/$1 [R=301,NC,L]
    # uploaded files
    RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

Topic Closed

This topic has been closed to new replies.

About this Topic