Apache - Rewrite the subdomain to a location? - php

I am sorry, I have tried for hours to get this working, but I haven't made progress...
I want to make it so that if a user on my site types user.site.com they will be taken to site.com/user, but the URL will still show user.site.com. How can I do this? With .htaccess? Server files?

Almost there Ken
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.site.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).site.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
%1 = what's before .site.com
$1 = what you got after the /
If you have test.site.com/foo.php , you would have /test/foo.php.
if you just want test, just forget about the $1.
QSA = query string append,
L = Last.
You should read the url about mod_rewrite in #phihag post.

Use mod_rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.site.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).site.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
If you want to link to resources, either use full (http://site.com/user/static/x.css) or relative (static/x.css) URLs. Absolute URLs (/user/static/x.css) will need to be crafted differently when this rule is in effect.

To keep the original address in the address bar you will need a reverse proxy rather than a redirect. Redirecting tells the browser to send a second request to the server with a different address, reverse proxy tells the server to find another page and send it without notifying the browser about it (this is what you want I believe). Reverse proxy is achieved with the [P] flag in mod_rewite
Make sure mod_rewrite, mod_proxy and mod_proxy_http are loaded and put the directives
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
RewriteEngine on
RewriteRule ^/(.*) http://site.com/user/$1 [PL]
into your virtual host configuration for user.site.com or .htaccess if you do not have root privileges. This will proxy all pages from the subdomain to the main domain folder. If you only want to proxy the index page use RewriteRule ^/ http://site.com/user instead.
I assume you are using http and not https. If so, it gets a little more complex...

Related

htaccess simulate subdomain to subdirectory in Wordpress

I need to develop a plugin that can write into the WP htaccess file and "simulate" subdomains.
I have a new WP installation with basic htaccess on example.com
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Is there a way via htaccess that if I go to about.example.com I see the page example.com/about, without changing the URL (meaning that I still see about.example.com in the browser address bar)?
And also, is it possible that if I go to about.example.com/category1 I see the page example.com/about/category1?
I tried using the following code by adding it to the end of the htaccess, but it doesn't work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com.com$
RewriteRule ^(/)?$ about [L]
You should be able to do that using the following rule in your .htaccess:
RewirieEngine On
RewriteCond %{HTTP_HOST} ^about.example.com [OR]
RewriteCond %{HTTP_HOST} ^www.about.example.com
RewriteRule ^(.*) http://example.com/about/$1 [P]
What this does is check for the address about.example.com, if the condition is met it should redirect to example.com/about/.... but keeping the original address.
The [P] flag causes the request to be handled by mod_proxy, and handled via a proxy request.
However, keep in mind that using mod_proxy can cause security issues:
Take care when constructing the target URL of the rule, considering
the security impact from allowing the client influence over the set of
URLs to which your server will act as a proxy. Ensure that the scheme
and hostname part of the URL is either fixed, or does not allow the
client undue influence.
Normally using .htaccess you cannot go from one domain to another domain keeping the original URL. It can be a security nightmare as mentioned above. However, since this is a subdomain it should work.
Make sure you clear your cache before testing this.
Alternative
You can use ProxyPass - which would be a more secure option. You can find more information on how to use this via the Apache Documentation via this link. But this would need to done on a level higher than .htaccess. Through the config files.
I hope this helps.

htaccess redirect to external website without changing address bar

I have a subdoman called, let's say:
cloud.mygizmo.com
But when someone navigates to this URL I want them to actually go to:
11.22.33.44/cloud
Which is on a completely different host from mygizmo.com and can't be moved.
In my .htaccess I have this:
RewriteEngine on
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^cloud\.mygizmo\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cloud\.mygizmo\.com$
RewriteRule ^/?$ "http\:\/\/11\.22\.33\44\/cloud" [L]
Which does do the redirect, but it still changes the address bar in the user's browser.
How do I make it so that if a user navigates to cloud.mygizmo.com they actually go to 11.22.33.44/cloud but the address bar still says cloud.mygizmo.com?
You can't. Redirection doesn't work like that.
You could proxy the data instead (which would be inefficient and increase your bandwidth costs).
If you have mod_proxy installed, you can use the P flag to reverse proxy on behalf of the browser:
RewriteEngine on
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^cloud\.mygizmo\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.cloud\.mygizmo\.com$
RewriteRule ^/?$ "http\:\/\/11\.22\.33\44\/cloud" [L,P]
You can also reverse proxy using ProxyPass or ProxyPassMatch but those will only work in the vhost/server config.
In the cloud.mygizmo.com/www.cloud.mygizmo.com vhost you can say:
ProxyPass / http://11.22.33.44/cloud
And then any request for the cloud.mygizmo.com gets proxied to the http://11.22.33.44/cloud host.
Note that ProxyPass works like Redirect, it links together the path nodes / and /cloud. So if someone were to go to:
http://cloud.mygizmo.com/foo/bar
They'd get reverse proxied to:
http://11.22.33.44/cloud/foo/bar
If that's not what you want, then use ProxyPassMatch:
ProxyPassMatch ^/$ http://11.22.33.44/cloud
Alternatively, if you want the rewrite rule to behave in the same way, you need to capture the request URI and pass it to the target with a backreference:
RewriteRule ^/?(.*)$ http://11.22.33.44/cloud/$1 [L,P]
probably not the ideal solution, but you could build a page that's pretty much a giant iframe and load the content inside the iframe...
Edit: See if Blue Host allows Parked Domains in your control panel (aka Masked Forward). I think this is what you want.

Mod rewrite base URL

I have several domains that I want to all rewrite to one domain. I don't want it to redirect because I want the URL to look like what the user has entered in. For example if they enter www.example.com I want it to load the page from www.sample.com/default.php?from=example
I have worked a little with rewriting if you have www.site.com/var1/var2 making it load www.site.com/index.php?one=var1&two=var2
Is it possible to do what I am looking for just through the .htaccess file? I tried looking around and couldn't exactly find what I was looking for
Thanks
If the sites are hosted on different servers or don't share a common document root, then you'll have to rely on mod_proxy and you can use the P rewrite rule flag. For example, these rules in an htaccess file in www.example.com's document root:
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ http://www.sample.com/default.php?from=%1 [L,P]
Will take the request http://www.example.com/ and invisibly proxy it to http://www.sample.com/default.php?from=example. The browser's URL address bar will remain http://www.example.com/.
Note that the rule only matches against the request URI /. If you want to do more, you'd have to create the correct regular expression and grouping.
If you have redirects on the sample.com site, you'll need to employ ProxyPassReverse to rewrite the redirects. Also see ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath if there are cookies involved.
If you can do this in vhost or server config instead, then consider simply using ProxyPass instead of mod_rewrite. The ProxyPass directive won't work inside htaccess files.
EDIT:
Seeing as how everything is in the same document root, you won't need to proxy anything. Simply:
RewriteCond %{HTTP_HOST} !^www\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^/?$ /default.php?from=%1 [L]
I use online generators.
Use this generator: mod-rewrite

How to set a URL rewrite for sub-domain links like these?

I have a site ( say by name www.manoj.com ) and I have created a forum on sub-domain
forum.manoj.com
Now, I want to redirect all the links following manoj.com/forum/{anything else} to forum.manoj.com/{anything else}
How can I do it.
Thanks
If you want to redirect example.com/forum to forum.example.com, in [document_root]/forum/.htaccess write:
RewriteEngine On
RewriteBase /forum
RewriteCond %{REMOTE_HOST} !=forum.example.com
RewriteRule (.*) http://forum.example.com/$1 [R=301,NE,L]
The R=301 flag is to indicate it is a "permanent" redirect, Apache will send the header HTTP/1.1 301 Moved Permanently.
If you really cannot add a .htaccess file there, in [document_root]/.htaccess write:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !=forum.example.com
RewriteRule forum/(.*) http://forum.example.com/$1 [R=301,NE,L]
But this is not enough. You need to set the DNS record for forum.example.com and set up virtual host (if you use Apache) on the server before it will work. Typically virtual hosts are defined in httpd.conf. For more see this

How to implement 301 redirection with PHP/Apache?

When user is visiting by www.domain.name, redirect to domain.name.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.name$ [NC]
RewriteRule (.*) http://domain.name/$1 [R=301,QSA,L]
Put this in an .htaccess file in your root directory of your website:
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.name
RewriteRule (.*) http://domain.name/$1 [R=301,L,QSA]
This is what they do, in order:
Turn the rewrite engine on
Make sure that HTTP_HOST was provided
If it doesn't start with the name without the www or any other sub domain, then allow the rewrite to continue. This prevents an endless redirect back to itself.
Grab everything after the URL (.*), including the querystring QSA, and redirect R=301 to the correct domain. The L just says this is the last command in the file if a match is found.
In Apache you add a Redirect line to your configuration files. In php you reply with a status code 301 and a Location: header.
However, a redirect requires an additional network round trip. Are you sure you don't want to just use a ServerAlias line so that the same content is served up whether they visit with or without the www.?

Categories