htaccess - Transform get parameter value in a subdomain - .htaccess

In my web application, I currently have URLs like this:
https://example.com/mypage?company=companyname&otherparameter=othervalue&...
I would like to transform the above URL this way:
https://companyname.example.com/mypage?otherparameter=othervalue&...
so basically transforming the value of the GET parameter "company" into a subdomain while preserving the other GET parameters in the URL (and preserving, obviously, also the path of the file on the server).
I also need to exclude the "/api" directory from this rule (so all files under the "/api" subdirectory should be served as usual).
I know I need to use .htaccess but I can't find a way to get it to work. If someone's got a hint, that would be very helpful.
Thanks!

This will capture the "subdomain name" from any incoming request and add it as query parameter to the internally rewritten target:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(?:www\.)?example\.com$
RewriteCond %{HTTP_HOST} !^([^.]+)?example\.com$
RewriteCond %{REQUEST_URI} !^/api/?
RewriteRule ^ %{REQUEST_URI}?company=%1 [QSA,L]
This will take care of handling incoming requests. This does not somehow magically change references you hand out, so links embedded in HTML markup or javascript for example.
You need to make sure that your http server actually responds to requests to those "subdomain" based host names. A default virtual host is usually used for such thing. You also need to take care that the DNS resolution of such names works and points towards your http server. And finally you have to provide a valid SSL certificate for all those host names. A wildcard certificate is an option here, but unlike normal certificates that does not come free of charge.
It is a good idea to implement such general rules in the actual host configuration of your http server. You can use a distributed configuration file for this (".htaccess"), but that comes with a few disadvantages.

Related

Redirect visitor to the same url that he/she write without database

i have 2 websites let says (example1.com and example2.com).
if the visitor write in the url any number for example (example1.com/123456) i want it to re-directed to (example2.com/123456) so the domain only replaced not the numbers the visitor written.
i need that happen using htaccess file or any simple methods without database or save any data because i have only HTML pages, and its hard to me to do it because i am still learning.
All you need is a redirection on protocol level. That is possible with all usual http server's, the exact solution depends on which http server you are using to serve the domain "example1.com".
Since you tagged your question .htaccess and mod-rewrite I assume that you are using the apache http server. If so you can implement a rule like that one:
RewriteEngine on
RewriteRule ^/?(\d+)$ https://example2.com%{REQUEST_URI} [R=301,L]
It will redirect all requests to example1.com that use a path that consists only of digits to the second domain while preserving the requested path. An external redirection will get performed using a http status 301 ("moved permanently") as a response to the first request to example1.com.
If both domains are served by the same http server and you do not have setup separate virtual hosts you probably have to add a condition to that to make sure the rule only gets applied to requests to the first domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www.)?\.example1\.com$
RewriteRule ^/?(\d+)$ https://example2.com%{REQUEST_URI} [R=301,L]
Preferably such a general redirection rule should get implemented in the central http server's host configuration responsible for serving example1.com. If you do not have access to that you can indeed use a distributed configuration file instead, often called ".htaccess". If so you need to enable that feature beforehand using the AllowOverride directive. The configuration file needs to be located in the hosts top level DOCUMENT_ROOT folder and it needs to be readable for the http server process.

I need help setting up .htaccess

I need to set up .htaccess. If the user clicks the "site.com/profile" link, I need to check if there is a "token" field in the user's cookies. If this field is not empty, I must let the user through, otherwise I redirect them to the "site.com/login" link.
You are probably looking for something like that:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !(^|;)?token=[^;]+(;|$)
RewriteRule ^ /login [R=302,L]
Obviously the rewriting module needs to be loaded into the http server. It generally is a good idea to implement such rule in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (often called ".htaccess"), but the consideration of such files needs to be enabled first and their usage comes with a number of disadvantages.

Rewrite URLs to subdomain using htaccess and wild card DNS

I want to change my website URL
http://domain.tk/site/google.com
To
google.com.domain.tk
I tried
RewriteEngine On
RewriteRule ^site/([^/]+)/?$ http://$1.domain.tk/ [NC,R=301,L]
it redirects to google.com.domain.tk and show the homepage content instead of the content in http://domain.tk/site/google.com
Example of what i want can be found on this website
The documentation I referred to in my comment to your question clearly states and demonstrates in examples that the order of arguments in a redirection rule is 1. matching pattern applied to the path component of the incoming request and 2. the target path or URL the request should internally get rewritten to. The rule you implemented does the opposite of what you ask: you implemented an external redirection to exactly that host name you want to rewrite from .
This example should get you going:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^google\.com\.domain\.tk$
RewriteRule ^ /site/google.com%{REQUEST_URI} [QSA]
For this to work the rewriting module has to be loaded into the http server, obviously and it has to be activated for the host. You should implement such rules in the actual host configuration inside your http server. If you do not have access to that (so if you are using a cheap hosting provider) then you can instead use a distributed configuration file (often named ".htaccess"), but that needs to be enabled in the host configuration first and it comes with a performance penalty.

How to redirect a URL in htacess with query string to the same URL but different query string

Because of removing several languages from a multi-language website, I need to 301 redirect pages that end with ?lang=da, ?lang=de, and ?lang=nl to the same URLs but ending in ?lang=en. It sounds like a common scenario, but I haven't found the right code yet to accomplish this or tried some code because the purpose of that code was either to redirect to one new URL or to replace the URL but not the query string, while I need to replace the query string and keep the URL.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=(da|de|nl)$
RewriteRule ^ %{REQUEST_URI}?lang=en [QSD,R=301,END]
For this to work the rewriting module obviously needs to be loaded into your http server and it has to be activated for your http host too.
It is a good idea to start out with a 302 redirection and to only change that into a 301 once you are convinced everything is set up as required. That way you prevent ugly caching effects...
You can implement such rules in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (".htaccess"), but that has performance disadvantages and you also need to enable the interpretation of such files first. Please see the documentation of the tool you are using to learn how to do that.

Redirect to url within another url using .htaccess

I have a problem, I need to redirect users to a URL within a url.
For example:
When the user visits the URL: api.example.com/redirect/www.google.com
I want the user to be redirected to: www.google.com
I've been trying for a while now, but no sucess.
You are probably looking for something like that:
Variant for the main http server configuration:
RewriteEngine on
RewriteRule ^/redirect/(.+)$ http://$1 [L,R=301]
Variant for a .htaccess style file:
RewriteEngine on
RewriteRule ^redirect/(.+)$ http://$1 [L,R=301]
It obviously requires the rewrite module being available inside your local http server.
In general you should prefer putting such rules into the main server configuration, if you have access to that. A .htaccess style file should only be used if you have no other alternative. The reason is that such files are notoriously error prone, hard to debug and really slow the http server down.
A more robust approach would be to take the captured part of the url and hand it over to a script on the server side as a parameter. The script can validate the part, whether it is a valid "url" (to keep things simple here...). And maybe make a head request to check of the target exists and is usable. Only then it redirects. Otherwise it returns a meaningful error message.

Resources