htaccess redirect htm to html (strange behaviour) - .htaccess

I have two hosting accounts with different providers.
both sites have the following in the htaccess.
RedirectMatch 301 ^(.*).htm $1.html
On one site (a VPS) it works as expected and
//www.example.com/page.htm
is correctly forwarded to
//www.example.com/page.html
But on the other hosting provider (shared hosting. Different provider) it removes the www from the url so goes to:-
//example.com/page.html
It only strips the www if the htm-to-html redirect occurs. So it's not all pages that have the www removed so it's not a global setting removing www from all urls.
Is that redirect syntax wrong? If so then why does it only affect the domain on one hosting provider. Or is it a setting somewhere else in the website setup?
Thanks

RedirectMatch 301 ^(.*).htm $1.html
If you don't explicitly include the hostname in the target URL (ie. specify an absolute URL) then Apache gets this information from the current server. By default, this will be the hostname used in the request (www, non-www, or whatever). However, if the directive UseCanonicalName On is set, the hostname as defined by the ServerName directive will be used. This is what I guess is happening.
Unfortunately, on a shared server, you are not going to be able to change this behaviour. The UseCanonicalName directive can only be set in the server config, not .htaccess.
I think the only resolution is to be explicit and specify the canonical hostname in the above directive:
RedirectMatch 301 ^/(.+)\.htm$ http://www.example.com/$1.html
Some people recommended to always specify an absolute target URL for redirects to avoid such problems. (Personally, I would only do this out of necessity.)

Related

Redirect any subdomain to same subdomain of different domain

I've moved my site from floriskleijne.nl to floriskleijne.com, and now want any visitor to any subdomain or subdirectory of .nl to be redirected to the .com equivalent of the URL. The challenge is in the subdomains: I have a number of those, and I want them to all be redirected to the equivalent subdomain on the new domain.
So for instance,
en2nl.floriskleijne.nl/ should redirect to en2nl.floriskleijne.com/
www.floriskleijne.nl/about/the-author/ should redirect to http://www.floriskleijne.nl/about/the-author/
floriskleijne.nl/about/the-author/ should also redirect to www.floriskleijne.nl/about/the-author/
I found the solution in this thread, but this does not seem to work for me:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.floriskleijne\.nl$
RewriteRule (.*) http://%1.floriskleijne.com/$1 [R=301,QSA,L]
There are two problems with this approach:
It doesn't work on my site: a subdomain call to the .nl domain gives me an "Apache is working normally" page at the URL I enter, with apparently no redirection at all. And a subdomain call with subdirectories and/or files gives me a 404 error.
Though this htaccess does the job of redirecting to .com, it seems to me that it ignores the no-subdomain version http://floriskleijne.nl. And indeed,iIf at all possible, I would want that to be redirected the same way (identical path). Oddly enough, floriskleijne.nl/whatever.subdir does get redirected properly.
I've checked the DNS, and I have an A record for both floriskleijne.nl and *.floriskleijne.nl, both pointing to the same IP.

htaccess subdomain redirect to non www

For our shop CMS we have a system that works like this:
When a customer registers they will get a subdomain on our root domain.
Something like: myshop.daretoshop.nl
For SEO we want the users to be redirected to the non www version of this subdomain when they enter www.myshop.daretoshop.nl, so they are redirected (301) to myshop.daretoshop.nl
Is this possible with htaccess? We want this so happend with all subdomains, so maybe its possible to set this globally?
thanks in advance.
Use mod_rewrite.
For mod_rewrite directives to work using .htaccess, you need to set AllowOverride FileInfo. Details about how to configure mod_rewrite for your specific needs can be found in one of the similar questions, such as Generic htaccess redirect www to non-www.
You also need to configure your DNS server such that the subdomains are actually resolved to the IP address of your web server.
BTW there are absolutely no SEO benefits in redirecting http://www.example.com to http://example.com.

Redirect all subdomains to subdomain at different domain

Hi I need to redirect all subdomains in a domain to the same subdomain but at a different domain. The best way im guessing is through a htaccess file but im not sure how the file would be.
Example:
sd1.example.net ---> sd1.example.com
sd2.example.net ---> sd2.example.com
sd3.example.net ---> sd3.example.com
But I need this to be done for all of the subdomains in example.net. Thanks.
If you have an Apache server running on example.net and the requests for all the subdomains look in the same parent directory you can do something like the following:
RewriteEngine On
### Find the subdomain part (it will be available in %1)
### Use one of the RewriteCond-s and delete the other one
# Only redirect subdomains, not plain example.net
RewriteCond %{HTTP_HOST} ^(.*)\.example\.net$
## Redirect both subdomains and plain example.net (uncomment to enable)
#RewriteCond %{HTTP_HOST} ^(.*\.)?example\.net$
# Find the path requested (it will be available in $0)
# This rule does not attempt to match the domain, only the path
# Redirect subdomain and path to example.com
RewriteRule ^.*$ http://%1.example.com/$0 [L]
I haven't tested this so it might be missing query strings, etc. It will also undesirably redirect https:// to http://. As long as you have a single .htaccess file that can affect all your subdomains this should work, or at least be a very good starting point. Check out Apache's mod_rewrite documentation for more information about how this works.
EDIT
Having recently wanted to do exactly this myself recently, I have worked out a short .htaccess file that does the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?olddomain\.com$
RewriteRule ^(.*?/)?public_html/(.*)?$ "http\:\/\/%1newdomain\.org\/$2" [R=301,NE,L]
It assumes the following file structure:
.htaccess
public_html/
+-content
lots/
+-public_html/
| +-content
of/
+-public_html/
| +-content
subdomains/
+-public_html/
+-content
My main site (newdomain.org) is in /public_html/. I have a number of subdomains, e.g. subdomains.newdomain.org which is in /subdomains/public_html/. This keeps all the files of each my subdomains completely separate from each other and my main site. (My hosting service recommends /public_html/, /public_html/subdomains/ but that means each subdomain is also accessible at newdomain.org/subdomains/ which is not what I want). The only restriction this gives me is that I can never have a subdomain called public_html, which I think you'll agree is perfectly acceptable.
The flags on the rule are as follows:
R=301 - Redirect with a 301 Moved Permanently code. You can change the code if you don't need a permanent redirect, e.g. 302.
NE - No Encoding - Don't URI encode the new address, i.e. keep % as %, not %25
L - Last - Stop processing rules
Note that the .htaccess file must be in the root directory of your web server, not in the directories with your content files. This is because the rewrite rule works at the file system level, not the URL address level.
An address:
any.subdomain.olddomain.com/any/address.html?any=query&you=like
is changed to:
any.subdomain.newdomain.org/any/address.html?any=query&you=like

Linking one domain to another domain with htaccess

I'm stuck with htaccess redirect on this case:
I have myapp.com domain where my main website and service runs on. My customers logs into their accounts on myapp.com and use. Now, I am going to provide one of the features on a separate domain, let's assume "goto.com". However, I don't want to build a separate app on goto.com. I just want to redirect all coming requests to goto.com to a php script under myapp.com but this redirection should be in the backend (masked), not a 301 redirection.
Let me clear up:
myapp.com - /var/www/vhosts/myapp.com/httpdocs/index.php
goto.com --> masked redirection --> myapp.com/goto.php?$1
How can I do this with htaccess? Any suggestions?
Just found that it can be done with redirect [P] (proxy) method: RewriteRule ^(.*)$ http://myapp.com/public_view/$1 [P] What do you think? Is this a good and stable method?
That method is fine, it utilizes the mod_proxy module. The only thing I can suggest is adding the L flag as well in case you have other rules in your htaccess file.
A limitation with using the P flag in an htaccess file is that if a page or request to http://myapp.com/ redirects, then you're URL address bar will say http://myapp.com/ instead of your other domain. The only way around this is to use ProxyPassReverse but it doesn't work in an htaccess file. You'd need access to vhost config:
ProxyPass / http://myapp.com/public_view/
ProxyPassReverse / http://myapp.com/public_view/
Additionally, if http://myapp.com/ sets cookies, you'll need to use the ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath directives to rewrite those.

htaccess folder to subdomain rewrite

How can example.com/blog seamlessly show the content of blog.example.com without a redirect?
Not this:
RewriteRule ^/blog http://blog.example.com [R=301,L]
Thanks.
In order for this to occur, both the domain and subdomain would have to be running from the same directory of files. Then you could just rewrite the /blog path to the same file that the subdomain is using to run. Depending on your permissions, it could be possible to specify a ../ path to the file that it's using if you get the directories right.
However, anytime you specify a full domain path in a rewrite rule, Apache will always redirect the request. You can't get around that.
if example.com/blog and blog.example.com(subdomain) both points to same directory(blog). Then you can directly access the specified path without any redirection.

Resources