I would like to change the link "http://blog.test.com/" to "http://www.test.com/blog/".
I've tried the following code in my .htaccess
RewriteRule ^blog.test.com?$ test.com/blog [NC,R=301,L]
Did I miss anything? Thanks
If you're using apache, you need to match the host part of the url (e.g. blog.test.com) in a RewriteCond:
RewriteCond %{HTTP_HOST} ^blog.test.com$ [NC]
RewriteRule ^(.*)$ http://www.test.com/blog/$1 [R=301,L]
first of all, you must replace http://blog.test.com/whatever_or_empty to http://www.test.com/blog/whatever_or_empty in your HTML hrefs.
blog.test.com although a sub domain, is a different URL. i.e. when a RewriteRule does a rewrite to another URL an external redirect will occur. This will reflect in the browser. Be a temporary redirect(302(the default)) or permanent redirect(301).
So, using url rewriting to change the link http://blog.test.com/ to http://www.test.com/blog/ is useless.
Although, you can achieve this using Apache Module mod_proxy.
The Apache Proxy Modules has these:
mod_proxy: The core module deals with proxy infrastructure and configuration and managing a proxy request.
mod_proxy_http: This handles fetching documents with HTTP and HTTPS.
mod_proxy_ftp: This handles fetching documents with FTP.
mod_proxy_connect: This handles the CONNECT method for secure (SSL) tunneling.
mod_proxy_ajp: This handles the AJP protocol for Tomcat and similar backend servers.
mod_proxy_balancer implements clustering and load-balancing over multiple backends.
mod_cache, mod_disk_cache, mod_mem_cache: These deal with managing a document cache. To enable caching requires mod_cache and one or both of disk_cache and mem_cache.
mod_proxy_html: This rewrites HTML links into a proxy's address space.
mod_xml2enc: This supports internationalisation (i18n) on behalf of mod_proxy_html and other markup-filtering modules. space.
mod_headers: This modifies HTTP request and response headers.
mod_deflate: Negotiates compression with clients and backends.
You need at-least mod_proxy and mod_proxy_http modules enabled for the proxy to work:
you should have lines similar to these in your apache's conf file:
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so
use this in your Virtualhost of http://www.test.com
ProxyPass /blog http://blog.test.com
ProxyPassReverse /blog http://blog.test.com
ProxyRequests On
ProxyVia On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
Definitions:
ProxyPass Apache Docs.
ProxyPassReverse Apache Docs.
ProxyRequests Apache Docs.
Proxyvia Apache Docs.
You can also use a cache with mod_cache: mod_cache.
For more on caching, refer here: mod_cache Apache Docs.
Related
I have a website for example: https://test1.com, and a page on that website https://test1.com/show-content.
I want that page to display the content from another website, for example https://test2.com/show-different-content.
I know I can do it with PHP and file_get_contents, but I'm trying to do it with .htaccess, since I understand it can be possible. I've looked through all the SO questions I found regarding that, but I found no clear solution.
What I have tried in .htaccess is the following:
<IfModule mod_rewrite.c>=
RewriteEngine On
RewriteRule ^show-content$ https://test2.com/show-different-content [P]
</IfModule>
What am I doing wrong? Or am trying to do something that is not possible?
You have two options for this if the owner of that second site actually grants permission to proxy his sites content. You can use the proxy module available for the apache http server.
Either direct:
ProxyPass /show-content/ https://test2.com/show-different-content/
ProxyPassReverse /show-content/ https://test2.com/show-different-content/
Or embedded in the rewriting module:
RewriteEngine On
RewriteRule ^/?show-content/(.*)$ https://test2.com/show-different-content/$1 [P]
Obviously the proxy module needs to be loaded and activated inside the http server.
I'm trying to redirect a URL with whatever folder is on the end to a new URL and I can't get the rewritecond figured out.
I'm trying to take URL like this:
http://www.example1.com/feature/this-folder-name/whatever1/whatever2
to
http://www.example2.com/whatever1/whatever2
The folder whatever1 can change as can whatever2.
I've tried:
RewriteCond %{HTTP_HOST} ^www\.example1\.com/feature/this-folder-name$
RewriteRule ^(.*)$ https://www.example2.com/$?&%{QUERY_STRING}
And
RewriteCond %{HTTP_HOST} ^www\.example1\.com/feature/this-folder-name$ [NC]
RewriteRule ^(.*)$ https://www.example2.com/$1 [R=301,L]
There are a number of issues here, so I took the liberty to clean things up a bit...
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example1\.com/$
RewriteRule ^/?feature/this-folder-name/(.*)$ https://www.example2.com/$1 [R=301,END,QSA]
In case you want to be more precise and really only redirect if two separate folders are specified as you explained this might point you into the right direction:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example1\.com/$
RewriteRule ^/?feature/this-folder-name/([^/]+)/([^/]+)/? https://www.example2.com/$1/$2/ [R=301,END,QSA]
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...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic 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 dynamic 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 dynamic configuration files (".htaccess"). Those dynamic 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).
<VirtualHost *:80>
ServerName example1.com
redirect / http://example2.com
</VirtualHost>
<VirtualHost*:80>
ServerName example2.com
</VirtualHost>
Then let example2.com do what it does in its own virtual host settings. The assumption here is example2 knows where it’s own files are, independent of example1.
I have the following problem:
I have an apache serving files under url.com/ and url.com/a
I also have a node-express server listening on port 3000, which is not publicly accessable.
Now, I would like to be able to access the node server for any url like url.com/b/.
My hosting company and google referred me to using .htaccess rewrites like so under url.com/, but it does not work:
RewriteEngine on
RewriteRule ^(.*)b(.*)$ https://url.com:3000/b/$1
Does the port need to be publicly accessable for the mod_rewrite approach to work?
What is the proper way to set something like this up?
Many thanks!
Try mod_proxy:
<Location /b>
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
unfortunately, since i deployed on a managed virtual server, i wasnt able to use Elvis' solution, modifying the httpd.conf file.
I ended up using this solution, modifying the .htaccess file in the folder public_html/b:
RewriteEngine on
RewriteRule ^(.*) http://localhost:3000/$1 [P]
I have seen a few similar questions on here, but none of the answers have seemed to get me where I need to be.
I have a site, say:
www.first.com
And another site called:
www.second.com
The second.com domain is on a different server than first.com, but they are for the same company. I am trying to merge the domains into www.first.com so that end users only see first.com for this particular brand.
There is a Java applet running on www.second.com/applet that needs to stay on its own server. However, I would like to only have one domain to access the contents on both www.first.com and www.second.com -- I have looked into a 301 redirect, but I am trying to only have ONE SINGLE domain for this. The problem with a 301 is that the URL changes and displays "www.second.com" in the domain, which is what I am trying to avoid.
I would like to have JUST www.first.com and if someone goes to "www.first.com/applet" it shows the content from "www.second.com/applet" but KEEP "www.first.com/applet" in the URL bar, but this is seemingly difficult to achieve with my level of knowledge.
I have tried this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?first\.com$
RewriteRule ^(.*)$ http://www.second.com/$1 [P]
to no avail. I get the following error:
Bad Request
Your browser sent a request that this server could not understand.
Size of a request header field exceeds server limit.
X-Forwarded-Server
/n
Apache Server at * Port 80
I have read a little about a reverse proxy or something that may work.
How would I properly configure this? I found this:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://example.com
ProxyPassReverse / http://example.com
ServerName sub.example.com
</VirtualHost>
From here: How to show content from other domain without changing the URL
But I am not trying to use a subdomain. Hopefully I have explained what I need here. Basically, merging two domains that need to remain on separate servers but be delivered from a single domain.
Thanks!
It is very easy:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^Domainyoustayat.tld
RewriteRule ^(.*) http://Domain-you-want-to-show-on-above.tld/$1 [P]
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.