Simple apache mod_rewrite just not working - node.js

What I wanted is really simple, but it's just not working.
First the application is running on Node.js, i.e., localhost:8080
And I use Apache mod_rewrite to route the requests to Node.js, like this
<VirtualHost *:443>
RequestHeader set X-Forwarded-Proto "https"
ProxyPreserveHost On
ServerName shopecific.com
RewriteEngine On
RewriteRule /(.*) http://localhost:8080/$1 [P,L]
...
</VirtaulHost>
Which works perfectly. Then I want to redirect one request to a static page, which is from https://shopecific.com/amzvar to https://shopecific.com/amzvar.html
But I tried the following rewrite rules, but none of those works:
# RewriteRule ^/amzvar$, /amzvar.html [R,L]
# RewriteRule ^/amzvar$, https://shopecific.com/amzvar.html [R,L]
# RedirectMatch 301 ^/amzvar$ https://shopecific.com/amzvar.html
Somehow those rules are just got ignored, and I always got the not found page. Note that "amzvar" is not a defined route on the Node.js. I understand that by the following rule in routers.js, it could be redirected to "not found".
<Route path="*" component={NotFound} status={404} />
However, the Apache Rule should be matched first, right?
What am I missing here?

Looks like this is related to Service Worker. Somehow React-Router V3 and Service Worker does not work together to handle undefined router (i.e., it will redirect to default route () regardless other rules even in the Apache config.

Related

htaccess mod_rewrite carry folder names over

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.

apache and node express with node rewrites

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]

Showing content from another server on a domain without changing URL

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]

Change url completely using .htaccess?

I have url file which is xxy.com/file1/file2/file3/file4/file5/something.pdf.
This url is on yyy.com.
I don't want my users to know that pdf files are stored in different place.
So here what I want to do is:
Displaying xxy.com into yyy.com
Final output will look like:
yyy.com/file1/file2/file3/file4/file5/something.pdf.
Or if possible to yyy.com/something.pdf
And I don't need redirect. Once users click on the link they are supposed to see different url not the original url.
If mod_proxy is enabled on yyy.com then put this code in your DOCUMENT_ROOT/.htaccess file of yyy.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?yyy\.com$ [NC]
RewriteRule ^(.+?\.pdf)$ http://xxy.com/file1/file2/file3/file4/file5/$1 [L,NC,P]
You need to use the P flag in mod_rewrite, which will internally pass the request to mod_proxy. That means you need to make sure both mod_rewrite and mod_proxy is loaded in your server config. Then you can use something like this:
RewriteEngine On
RewriteRule ^something.pdf$ http://xxy.com/file1/file2/file3/file4/file5/something.pdf [L,P]
If you need change HOST you can use mod_proxy module with ProxyPass directive
Put this lines in .htaccess on example.com
ProxyPass /something.pdf http://example.com/file1/file2/file3/file4/file5/something.pdf
I use example.org because example.org are restricted here
But you must check for mod_proxy enabled.
Or you can use php/other languages script to read remote file.

Linking one domain to another domain with htaccess

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.

Resources