How can I redirect non www http to https www in web.config - url-rewriting

I want to redirect all of the following types of requests to https://www.example.com
http://example.com
http://www.example.com
https://example.com

This depends on how you want to redirect. You could...
Redirect from the client side. This is easy to do, simply add a script tag in the <head> of the page HTML document that contains the following.
let excluded = ['http://www.example.com', 'https://example.com', 'http://example.com']
if (excluded.indexOf(location.origin) !== -1) {
location.href = 'https://www.example.com'
}
There is only one problem with this code, being that the browser might complain about too many redirects. The second option is safer.
If you know how, you could use .htaccess. You will find this file in the root directory of your web server. Add the following lines:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
(Check out this question for more on this.)
If the page is not strictly static, you could redirect from the backend. There are a number of ways to do this, the most likely being to just use an npm module such as forcedomain (my personal favorite) to redirect when the request is made, rather than when the page loads. This is more efficient and the browser will not get cranky on you.
Use multiple CNAME records. If you aren't familiar with DNS, I would disregard this part of the answer. Basically, make a CNAME where the host is #, and the value is www. This will get all requests to the apex domain (example.com) and redirect them to the subdomain (www.example.com). When it comes to the protocol issue, there are a few ways of doing this, the best option being wildcard redirects (if your registrar provides this).
As a general rule of thumb, it's best to use DNS records, the .htaccess file, or some sort of backend plugin if possible for redirects if you know how. This all depends on your hosting, the nature of your website (static or dynamic), and your level of knowledge on these sorts of things.

Recommend using Firebase Hosting: https://firebase.google.com/docs/hosting/quickstart

Related

How to enable the Laravel app working in a subdomain to handle the requests from another subdomain, without changing the URL in the browser?

I have my Laravel app working in the admin subdomain:
admin.mysite.com
I would like to enable my Laravel app to also handle the requests coming from another subdomain; client.mysite.com, for example. I'm trying to utilize the Apache Rewrite feature for this; thus, I've created the following .htaccess file in the main directory of the "client" subdomain:
# file: /client/public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?([a-zA-Z][a-zA-Z0-9]*)/?(.*)$ https://admin.mysite.com/bu/$1/$2 [L,NC]
</IfModule>
That works, but I definitely don't want the URL to be changed in the browser. To that end, I tried the [P] flag to no success. Eventually, I removed the https:// part to avoid redirects*:
RewriteRule ^/?([a-zA-Z][a-zA-Z0-9]*)/?(.*)$ admin.mysite.com/bu/$1/$2 [L,NC]
But then, I get the following error:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
How to fix the error and make it work? And, is this the right way to accomplish such a task at all? (does it work with POST and all? Is there any better solutions?)
* If you start the substitution argument in the RewriteRule with https:// it will cause a Redirection (it's just like an External Redirect using the [R] flag). This will cause the browser to make a new request, hence changing the URL showed in the address bar.
Also, please consider that my app is on a shared host, thus I might not have access to some low-level features.
Set the DOCUMENT_ROOT of your client.mysite.com to point to the same directory where the main Laravel app resides (in your case, set it to /admin/public). Then utilize the subdomain routing facility in Laravel; the subdomain may be specified by calling the domain() method:
Route::domain('client.mysite.com')->group(function () {
Route::get('{username}', function ($username) {
//
});
});

How to redirect example.com and example.com/anything to example.com/blog

I want to redirect example.com and example.com/anything to example.com/blog. Please note few things.
I refer example.com for a 1 domain.
I use apache as web server.
My document root is set to /var/www/html/public within apache vhost conf file (For a laravel APP).
I tried setting redirects in .htaccess and using apache vhost conf file and I get redirect too many times error.
Can someone help me to accomplish this please?
This probably is what you are looking for: rewriting on the level of the http server:
RewriteEngine on
RewriteRule ^/?$ /blog [R=301]
RewriteRule ^/?anything/?$ /blog [R=301]
If by "anything" you actually mean anything so that a redirection should get applied regardless of the requested path, then this should do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^ /blog [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...
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).
You can do that in your routes
// web.php
Route::redirect('/', '/blog');
Route::redirect('/anything', '/blog');

mod_rewrite to keep subdomain in URL

I have this application that I want to support multi languages.
I thought the easiest way would be to use sub domains aka
http://fr.domain.com/content
Now I created the sub domain on my server, pointing to the main root and indeed, the above URL is accessible.
The problem now are all my links, which are absolute.
Is there a way with mod_rewrite to catch the language from the URL and than rewrite the links to the same sub domain URL?
So if we are on http://fr.domain.com/content and click the link http://domain.com/link I want the page to load as http://fr.domain.com/link
Is that possible?
Cheers!
You would probably have to check HTTP_REFERER if you want to do this through apache. It might be good to start updating the site so that the links are dynamic in the future...
Something like (I can't really test this currently):
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_HOST} !^[a-z]{2}\.host\.com$ [NC]
RewriteCond %{HTTP_REFERER} ^http://([a-z]{2})\.host.com/.*$ [NC]
RewriteRule (.*) http://%1.host.com/$1 [R,L,QSA]
EDIT: removed a NOT in the 3rd condition
You might have to check some other conditions, but test things out to figure out what works. Plus, if you do other redirects you need a way to maintain the original referrer. In some ways even with the links the way they are, it may be easier to do this through a more dynamic means with php (through session) or something.
I was using these:
http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_REFERER
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html
You probably need some kind of on-the-fly HTML rewriting tool like mod_proxy_html. This tool was specically designed for rewriting the links in pages on the other side of a reverse proxy but it should be possible to use it for generic link rewriting. Specifically the docs say:
Normally, mod_proxy_html will refuse to run when not in a proxy or when the contents are not HTML. This can be overridden (at your own risk) by setting the environment variable PROXY_HTML_FORCE (e.g. with the SetEnv directive).
The module is quite configurable and supports conditional rewriting and regexes so with some tweaking it should do what you want.
So you're saying your website has links like <a href="http://domain.com/link"> instead of just <a href="/link">??? Is there some reason your links are coded like that?
Would you be happy with whipping out a text editor and search/replace'ing those hrefs instead of doing something a-typical or excessively complex with URL rewriting?

joomla - SEO settings and mod_rewrite

I'm using Joomla 1.5.14 and I configured SEO as in the following image
Now I need to map a few old URL to the new site
let's say that I need to map htp://mysite/old.html to the new Joomla page
http://mysite/index.php?option=com_content&view=article&id=32&Itemid=70
I added in my .htaccess file the following
RewriteRule ^old\.html$ index.php?option=com_content&view=article&id=32&Itemid=70 #works!!
this works fine, but if I use the SEF URL in .htaccess (let's say the above page can be reached with htp://mysite/contacts.html), I obtain a 404 error
RewriteRule ^old\.html$ contacts.html #this does not work
Now the question:
Is it possible use SEF URLs in RewriteRule? where am I wrong?
thank you in advance
stefano
I think the problem is because Apache rewrites old.html to a page that doesn't actually exist, but rewritten in a different rule.
If you truly want to "rewrite" - in other words, have the page stay as old.html in the browser - then you don't need to do anything.
However to avoid duplicate content it's probably better to do a 301 redirect:
Redirect 301 old.html http://yoursite.com/contact.html
(You may need a forward slash at the front of old.html)

URL Masking with DNS and/or Mod_Rewrite?

I want to setup a system so that multiple custom domains like [mydomain.com/params] will redirect to [myapp.com/mydomain.com/params]. Can I do this using only DNS settings?
I'm guessing that is not possible, so would it be a better solution to direct mydomain1.com, mydomain2.com, mydomain3.com, etc. to one IP address then use Mod_Rewrite to direct each request (invisibly) to myapp.com/mydomain#.com/params ? Each redirected URL leads to content that is loaded from a centrally hosted CMS.
Any suggestions, resources, and/or solutions would be greatly appreciated!
Here's the solution:
Set DNS Address records for all vanity domains to the same IP address (so d1.com, d2.com, d3.com, etc. all have DNS A records set to one IP or FQDN for example)
Setup the server with one VirtualHost using the IP as the domain
Within that VirtualHost's root directory, create a .htaccess that sets up the mod_rewrite
Use the following for the mod_rewrite in the .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*) [NC]
RewriteRule (.*) http://myapp.com/%1/$1 [P,R=301,L]
%1 = the domain that is requested, so www.d1.com or d1.com
$1 = the rest of the URL that comes after the vanity URL (d1.com/everyting/else
This config invisibly redirects all requests.
Examples:
d1.com => returns content from => myapp.com/d1.com/
www.d1.com => returns content from => myapp.com/www.d1.com/
d1.com/blog/post/1 => returns content from => myapp.com/d1.com/post/1
No, you can't use only DNS for that.
If every domain can run standalone (www.domain.com) this would be a straightforward multi-site setup and does not require mod_rewrite, just a bunch of <virtualHost> directives that point to each site.
If you need exactly the setup you describe (http://www.hostname.com/www.2ndhostname.com/directoryname) you would need one <VirtualHost> with all the domains as aliases, and a mod_rewrite based redirect to point incoming requests to the right directory.

Resources