Asked  1 Year ago    Answers:  5   Viewed   7 times

I'm darn nearly pulling my hair out trying to figure this out tonight. I'm hoping someone can help me out.

I have 3 TLD's for a site, similar to the following:

  • www.domain.com
  • www.domain.org
  • www.domain.net

They are all located in the same directory.

I would like to set up 301 redirects so that all pages of the .org and .net point to their respective pages at the .com location.

For example, domain.net/topic/page as well as www.domain.net/topic/page should permanently redirect to www.domain.com/topic/page.

Currently, I am using the code below which only redirects the .net and .org home pages to the .com home page.

RewriteCond %{HTTP_HOST} !^(www.)?domain.com$ [NC]
RewriteRule .?$ http://www.domain.com%{REQUEST_URI} [R=301,L]

Thank you for your time,

Casey

 Answers

1

This one is based on your example with minor modification:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.)?domain.com$ [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,NE,L]

NC, R=301 and L are pretty obvious. The NE is for no-escape and it prevents query string variables from being escaped twice. The ^(.*) does not need a / in most cases.

Note: 301 permanent redirect responses will be cached by the browser so clear your browser cache every now and then while testing. Otherwise you may not see the result of changes you make.

Thursday, April 1, 2021
 
5

If you are using the R flag you are telling mod_rewrite that an external redirect is what you want, therefore the browser is asked to make a new request and the address bar should change accordingly.

Without the R flag, there is no redirect, but an Apache-internal request rewrite which is hidden from the browser. Thus, the address bar won't change. However, you cannot use internal redirects to external URIs for obvious reasons.

Since you seem to use an internal redirect anyway, just remove the R flag and it should work:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)$ ?url=$1 [L]
Thursday, June 24, 2021
 
Andres
 
5

Besides redirecting the request, you probably want to make sure the new url actually works too. You'll need both an external redirect and an internal rewrite for that. In the example below I use the THE_REQUEST trick to only trigger the rule if it is the actual request url, not if it is rewritten internally. It is required to prevent an infinite loop.

#External redirect with THE_REQUEST trick; change R to R=301 when everything works correctly
RewriteCond %{THE_REQUEST} ^(GET|POST) /index.php?tag=(.*) HTTP
RewriteRule ^ /%2? [R,L]

#Internal rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?tag=$1 [L]
Thursday, August 5, 2021
1

AFAIK you can't do redirection for the static assets, since GAE serves them directly according to the .yaml file rules, without even hitting your app code.

You could add a module (let's call it redirect-module for example) to your app, route ALL old domain URLs to it using a dispatcher file and use a dynamic handler in this module to redirect URLs to the new domain equivalents, along the lines suggested in the answers to the question you referenced. The new domain requests will continue to work unmodified, served either as static assets or the existing module(s) of your app. The dispatch.yaml file would look like this:

application: your-app-name
dispatch:
  - url: "your.old.domain.com/*"
    module: redirect-module

Another thought that comes to mind (I didn't actually do this, so I'm unsure if it would address your problem) is to avoid the redirect altogether and instead of mapping your app to 2 different domains map it only to the new domain and make the old domain a DNS CNAME/alias to the new domain.

Sunday, November 7, 2021
 
1

Try

RewriteCond %{HTTP_HOST} ^xxx.yyy.com
RewriteRule (.*) http://zzz.com/$1 [R=permanent,QSA,L]
Saturday, November 13, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :