1

I have my main site as example.com and also I am running the below subdomains with help of subdomain module

  1. one.example.com
  2. two.example.com

I have created the html block(admin/structure/block/add) for footer to list my site links. This block is visible for main domain(example.com) and all subdomains(one.example.com,two.example.com)

But I have URL issues in that footer block in subdomains. For example I have <a href="/suggestions">Suggestions</a> in footer block, when I am in main domain it goes to http://example.com/suggestions. It goes to http://one.example.com/suggestions & http://two.example.com/suggestions when I am in subdomains.

BUT IT SHOULD GOES TO ONLY http://example.com/suggestions FOR ALL SUBDOAMINS

Any way to achieve this?

1

I like adding the redirect to sites/default/settings.php instead of .htaccess (just because Pantheon ignores .htaccess redirects and a lot of my sites are hosted on Pantheon). I'm also just more comfortable with the syntax.

The first example will redirect just /suggestions to example.com/suggestions. The second example redirects any path beneath /suggestions (e.g., /suggestions/submit or /suggestions/submit/thankyou) to that same path on the main domain (example.com).

In terms of wild-carding this solution for scenarios similar to 'suggestions', I don't think that would work. I mean you don't want to redirect everything so you have to check for a specific REQUEST_URI.

But you could use this solution as a template for the other redirects you need. If you have a very large number of these (or anticipate a large number in the future), you might consider switching to the Domain Access module instead as it would handle this scenario without any redirects or custom coding.

You'll need to update 'example.com' to match your main domain and you may need to change 'http' to 'https' is you're using TLS.

// 301 Redirect from /suggestions to example.com/suggestions
if ($_SERVER['HTTP_HOST'] != 'example.com' &&
  $_SERVER['REQUEST_URI'] == 'suggestions' &&
  (php_sapi_name() != "cli")) {
    header('HTTP/1.0 301 Moved Permanently');
    header('Location: http://example.com/suggestions');
    exit();
  }

// 301 Redirect from /suggestions/* to example.com/suggestions/*
if ($_SERVER['HTTP_HOST'] != 'example.com' &&
    $_SERVER['REQUEST_URI'] == 'suggestions' &&
    (php_sapi_name() != "cli")) {
      $url = $_SERVER['REQUEST_URI'];
      $url = substr($url, 13); // stripping '/suggestions/' which is 13 chars
      header('HTTP/1.0 301 Moved Permanently');
      header('Location: http://example.com/suggestions/'. $url);
      exit();
  }
0

Here is the Rewrite Rule to be written in .htaccess file located in Drupal Root directory.

RewriteEngine on
# Rule for /suggestions.
RewriteCond %{REQUEST_URI} /suggestions
RewriteRule ^(.*) http://example.com/suggestions [L,R=301]

# Rule for /site-map.
RewriteCond %{REQUEST_URI} /site-map
RewriteRule ^(.*) http://example.com/site-map [L,R=301]

#Rule for more links.
...
...
  • Thanks. But I have more links as like /suggestions. So how can I add it for all links? – Mesk Jan 6 '17 at 12:01
  • @m.selvaKumar use a wild card .*, so try with RewriteCond %{REQUEST_URI} /.* and RewriteRule ^(.*) http://example.com/.* [L,R=301] – No Sssweat Jan 7 '17 at 2:49
  • The above code is not working. Its giving this error : The example.com page isn’t working example.com redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS – Mesk Jan 7 '17 at 2:49
  • @m.selvaKumar sorry that was just a guess. Somehow you need to use a wildcard, I think. – No Sssweat Jan 7 '17 at 2:53
  • @NoSssweat If I use that wildcard(.*) means it will applies for all links in that subdomain. But I need that for only some links in subdomains like /suggestions, /site-map, etc as mentioned earlier. Any way to fix this? – Mesk Jan 7 '17 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.