One website / three languages / three domains - .htaccess

I've got one website with three languages and three domains:
cheese.org, fromage.org and kaese.org
I want to create the following redirects (whith wildcards *)
cheese.org/fr/* redirects to fromage.org
cheese.org/de/* redirects to kaese.org
fromage.org/en/* redirects to cheese.org
fromage.org/de/* redirects to kaese.org
kaese.org/en/* redirects to cheese.org
kaese.org/fr/* redirects to fromage.org
I fiddled around with htaccess redirect (multidomain multilanguage) subfolder wildcard but didn't get it right.
Any suggestions?

Seems pretty straight forward... Depending on the http host you need to redirect incoming requests:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cheese\.org$ [OR]
RewriteCond %{HTTP_HOST} ^kaese\.org$
RewriteCond ^/?fr/(.*) https://fromage.org/$1 [R=301]
RewriteCond %{HTTP_HOST} ^fromage\.org$ [OR]
RewriteCond %{HTTP_HOST} ^kaese\.org$
RewriteCond ^/?en/(.*) https://cheese.org/$1 [R=301]
RewriteCond %{HTTP_HOST} ^cheese\.org$ [OR]
RewriteCond %{HTTP_HOST} ^fromage\.org$
RewriteCond ^/?de/(.*) https://kaese.org/$1 [R=301]
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Related

Redirect 301 htaccess for specific domain

I try several things for my redirections but without working solution.
I have 2 domains pointing in the same folder/website (mulitshops prestashop) and I would like to redirect some page from the first domain on a specific page and keep domain.
My work :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
But I cant do this with simple Redirect 301 because I can't specify the domain from request URI.
Beause the target page it's not the same according to the domain.
Sorry for my english and thank you in advance for your help.
In addition to what #DusanBajic explained in his comment to your question you also need to consider the difference between absolut and relative path in rewriting rules. This is actually explicitly documented...
WHen implemented in distributed configuration files (".htaccess") the rule pattern is matched against the relative path of the requested URL. You however try to match it against an absolute path which will never match. So either change your patterns to use relative paths too, or, preferably, implement your rewriting rules such that they work in both cases. So also when the rules are implemented in the real http server's host configuration where the pattern is matched against the absolute path inside the requested URL. This appears confusing at first. But it does make total sense, once you think about it.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out..
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How can I Redirect subdomain with path to root domain without this path

I need help with this case
I need to redirect (301) a subdomain test.example.com/?preview_theme=niss-promag
to https://example.com without /?preview_theme=niss-promag
I already used code but it's redirecting to root domain with the same path https://example.com/?preview_theme=niss-promag
This is the code currently used:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ https://example.com [L,R=301]
Note: I need to redirect whatever path of the subdomain to root domain I don't specify the one above!
This should be working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com [QSD,R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
If that does not work then most likely your http server is outdated, try something like that:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com/? [R=301]
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

301 Redirect - Different Location for Root and Subfolders

I have a Domain which I want to redirect to New Domain using 301 redirection on htaccess; I did that successfully but now the problem is, I want to redirect pages / folders of old domain to various locations of new domain. E.g.
www.olddomain.com redirect to www.newdomain.com (works)
www.olddomain.com/about should redirect to www.newdomain.com/who-we-are (doesn't work)
I tried redirect 301 oldurl newurl on my htaccess but it won't work as root already redirects to new domain.
is there any way to solve this issue?
Certainly what you ask is possible. Most likely you had an issue with the order of your rules if things did not work for you. You need to keep in mind that rules are processed from top to bottom. So more specialized, more detailed rules have to come first, more general rules further down.
Here is an example for the single example you gave:
RewriteEngine on
# this is a specialized rule for a specific resource
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/about/?$ https://newdomain.com/who-we-are [R=301]
# this acts as a fallback for a request not yet redirected
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/?$ https://newdomain.com/ [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
These rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
You can use this :
RewriteEngine on
#redirect specific pages to specific location on new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?about/?$ http://newDomain.com/who-we-are [NC,L,R=301]
#redirect the old domain root to new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?$ http://newDomain.com/$1 [NC,L,R=301]
You can remove the %{HTTP_HOST} condition in both rules above if your domains (old and new) are on different servers or pointing to a different document root on the same server.
Thanks, everyone, issue is resolved.
RewriteEngine on
RewriteCond %{HTTP_HOST} hitechos\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ https://www.hitechdigital.com/ [L,R=301]
Regards
Manoj Soni

.htaccess permanent redirect with query string

I want to have a permanent redirect where whoever visits the old url is automatically sent to the new one:
From: https://example.io/users?username=value
To: https://example.io/value
The new URL already works, but I just need the redirect. I already have this code:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ users.php?username=$1 [QSA,L]
I'd say this should roughly be what your question asks for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^username=(.+)$
RewriteRule ^/?users\.php/?$ /%1 [R=301]
Those lines should work likewise in the http servers host configuration or in dynamic configuration files (".htaccess").
It redirects all incoming requests to the old URL "/users.php?username=value. I fail to see how the code you posted should help here, which is why I wrote above rules from scratch instead of modifying yours.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect a certain page on domain A to a certain page on domain B

How would one redirect a certain page on domain A to a certain page on domain B in an htaccess file? We own both domains and both have nameservers pointing to a single server.
For example:
domainA.com/testpage
should go to
domainB.com/bestpageever
The following is as far as I have gotten. (I'm also forcing the www in front of the domain)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domainA.com$
RewriteRule ^(.*)$ http://www.domainB.com/bestpageever [R=301,L]
This happens to redirect all pages on domainA to the specified page on domainB, which is not what I want. No matter what kind of variation I try, I can't seem to get it to redirect just "testpage" on domainA to "bestpageever" on domainB.
This probably is close to what you are looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainA\.com$
RewriteRule ^/?testpage$ http://www.domainB.com/bestpageever [R=301]
The above rule will work in the http servers host configuration or in dynamic configuration files (.htaccess style files).
If you place such rules in the http servers host configuration you can simplify those rules to this, since then the rules are only applied inside the specific http host they are configured in:
RewriteEngine On
RewriteRule ^/testpage$ http://www.domainB.com/bestpageever [R=301]
A general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Resources