subdomains pointing subfolders with mod rewrite - .htaccess

Hi I'm quite new to PHP... And need something to do as fast as I can..
I have some clients within the "clients" directory like here...
"http://domain.com/clients/client0001/fluids/..."
I want this URL to be shown in the address bar like this.
"http://client0001.domain.com/fluids/..."
with the help of .htaccess. Any help will be appreciated ...
Thanks

You can use mod_rewrite to rewrite such URLs:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
RewriteRule !^clients/ clients/%1%{REQUEST_URI} [L]
But your webserver needs to be configured so that it accepts such host names and sends the requests to the proper virtual host.

I've used this before as posted by Gumbo.
Only issue I've had with it is that you can't then use further rewrite rules - so if the site you are serving on the subdomain uses url rewriting, you have to make it its own virtual host.

Related

How to redirect domain.tld/fr/anything to domain.tld/FR/anything?

I have a website setup at mydomain.com/FR
I'd like to make sure people typing
mydomain.com/fr are being sent to mydomain.com/FR
mydomain.com/fr/aNyTHiNg are being sent to mydomain.com/FR/anything
... whether they type http, https or http(s)://www.
How would I do that using .htaccess ? I'm afraid this is too much of a complex rule for me to sort it out.
You can use this simple rule in your site root .htaccess:
RewriteEngine On
RewriteRule ^fr(/.*)?$ FR$1 [L]

URL masking (?in .htaccess) from one domain to another

I've been searching the archives but I can't find anything that is making too much sense to me.
I have a site with a couple of subdomains which redirect to other sites.
E.g.
the visitor types - www.jmp.redtwenty.com.au - and is redirected to - http://creator.zoho.com/redtwenty/jmp-conversion-tracking
Is there any way to mask this redirect so that the visitor still sees jmp.redtwenty.com.au in the address bar?
I keep seeing mention of a rewrite rule in .htaccess but not sure if that is what I want.
Thanks
Mike
You can do this a few ways, but you'll need to make sure mod_proxy is enabled.
If you have control of the server config or the vhost config of the www.jmp.redtwenty.com.au/ domain, you can add this to it:
ProxyPass / http://creator.zoho.com/redtwenty/jmp-conversion-tracking/
Or in the htaccess file in the document root of http://www.jmp.redtwenty.com.au/:
RewriteEngine On
RewriteRule ^(.*)$ http://creator.zoho.com/redtwenty/jmp-conversion-tracking/$1 [L,P]

URL Rewriting subdomain

I'll be quick on what im trying to do,
basically I have a user profile page that will be my url, let's say,
profile.php?user=alex
So now what is working fine in my .htaccess file is changing that into
website.com/alex
for quicker access.
For other purpose, I would need that to be basically
alex.website.com
but I couldnt figure out a way to rewrite my URL to that, instead of having a subdomain for every user.
If you have any idea if it's possible & how I would go on doing this, I would appreciate it alot!
Thank you
Alex
To just rewrite the URL path, try this rule:
RewriteRule ^[a-z]+$ profile.php?user=$0
If your user names have a different syntax, replace [a-z] as you need.
For rewriting the host, try this rule:
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example\.com$
RewriteRule ^$ profile.php?user=%1
Note that this will only rewrite //alex.example.com/ internally to //alex.example.com/profile.php?user=alex. Additionally, your server will already need to be configured that it accepts any subdomain of your domain (see ServerAlias and name-based virtual hosts).

How can I redirect to a different domain without changing the URL in the address bar?

I want to redirect from:
domain1.com/photos
To:
domain2.com/photos
I want the URL in the address bar to still read:
domain1.com/photos
Is there a way to do this using only .htaccess?
Note:
My .htaccess file for domain1.com is currently completely blank.
No, there isn't a way to do this with .htaccess. Doing so would present a glaring security hole - imagine someone doing this with a bank's website!
If both are hosted on the same server, do this in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule (.*)$ http://www.domain2.com$1 [P]
</IfModule>
If you own both domain1 and domain2, you could accomplish this through domain name forwarding. Check your domain name registrar (like godaddy.com) for the options.
No, you can not do it through htaccess file.
You could open an iframe in domain1.com/photos that shows the contents of domain2.com/photos. But then the url would never change from domain1.com/photos, even when you went to a different page in domain2.
What are you trying to do? It sounds very sketchy. Do you own both domains? Why would you want to duplicate the contents of one site at another address?
Why is this not possible? Seems like a reasonable task as long as your Apache has mod_proxy installed:
ProxyPass /photos http://domain2.com/photos/
http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypass
A way around it, if the page at domain.com/photos is a server side script, do an HTTP call and serve up the response.
In ColdFusion:
<cfhttp url="another.domain.com/photos">
<cfoutput>#CFHTTP.FileContent#</cfoutput>
They'll be an extra request, but it'll get you the output you want.
its impossible using htaccess file.

.htaccess and rewrite of URL

We have the following example "ugly" URL:
https://some.uglyurl.com/directory/test.jsp?hotelid=1111&rateplanid=33333
we need to direct our customers to the above URL using our own domains URL as the address - so it would look something like:
https://www.PrettyURL.com/reservations?hotelid=1111&rateplanid=33333
The idea being that the address our customers "see" is a nice looking "familiar" URL to them. Is this possible in .htaccess? We would tack on various variables AFTER the test.jsp in the ugly URL - so it can't just be a fixed set of variables.
many thanks for any help.
If you were just using plain HTTP, you could set up the pretty URL server as a proxy that passes every request to the ugly URL server and the response back to the client:
RewriteCond %{HTTP_HOST} ^pretty\.example\.com$
RewriteRule ^reservations$ http://ugly.example.com/directory/test.jsp [L,P]
But as you’re using HTTPS, it is not possible without getting an error message, that the certificate’s host name is not correct.
This is the code that worked for me:
RewriteCond %{HTTP_HOST} ^www.prettyurl.com$
RewriteRule ^reservations$ https://uglyURL.com/istay/istay.jsp?%2 [QSA,L]
It works exactly as I needed it.
Many thanks to Gumbo and others for all their help.

Resources