Change on `apache2/sites-enabled/000-default.conf` not working - apache2

I change one of the redirect permanent but it seems that there is some kind of cache:
<VirtualHost *:80>
...
Redirect permanent /some http://example.com
to
<VirtualHost *:80>
...
Redirect permanent /some http://newexample.com
on incognito is working but I can't ask everyone to use incognito.
I found that you could add:
Header always set Cache-Control max-age=0
Header always set Expires "Thu, 01 Dec 1994 16:00:00 GMT"
but that will apply to every request, right? It will load the system a lot so I want to set this header only to redirects or maybe the actual approach is bad and I need to do another thing.

Related

Clear the Browser Cache of React app after every new deployment

I have added Cache Headers but Its doesnt remove my Browser Cache. Can anyone please help how to do that??
<VirtualHost *:443>
ServerName <ip-address>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/build
Include conf/headers.conf
SSLEngine on
SSLCertificateFile /etc/httpd/sites-available/cert.crt
SSLCertificateKeyFile /etc/httpd/sites-available/ssl.key
<Directory /var/www/html/build/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
There are many different browsers, there is no 100% reliable way for you to control what a clients browser stores in it cache. You can set headers on your servers response which tell a web browser that it should dump old files from it's cache and request new ones after a predefined period but this is only a recommendation, the browser may choose to ignore it, after all you do not control the browser software a client is using.
Set browser cache control headers in your Apache config file something like this...
# here we are telling the browser to request new files for specific images after 604800 seconds or 7 days, i.e. dump the cache if files are more than 7 days old
<FilesMatch "bird.jpg|dog.jpg|noob.jpg|fish.jpg|rabbit.jpg|cow.jpg">
Header set Cache-Control "max-age=604800, public, must-revalidate"
</FilesMatch>
# here we are telling the browser to request new files for specific file types after 86400 seconds or 1 day
<FilesMatch ".(css|js|ttf|ico)$">
Header set Cache-Control "max-age=86400, public, must-revalidate"
</FilesMatch>
If you are just looking for a quick way to clear the cache on google chrome on your own pc you can install a browser plugin to to that such as https://chrome.google.com/webstore/detail/no-cache-refresh/njacldedajpdoofkhpfckdkgcahjlfkj

Using Create-React-App with Apache VirtualHost

I am trying to develop a project for a company. This company has an API that only allows requests from two sources: Their own host, and xyz.localhost.
At first, I developed the project with jQuery only (loaded in via CDN). With an Apache VirtualHost setup, this worked -- I could access the API.
Now, I want to refactor the project and use React with it. I used create-react-app to create a react directory.
The problem is: I can get create-react-app to use xyz.localhost, but I am still getting the CORS error message in Chrome:
Access to fetch at 'http://api.thatcompany.com/search?search=a'
from origin 'http://xyz.localhost:3000' has been blocked by CORS policy
Has anybody any ideas how to make it work?
Thank you in advance.
/private/etc/apache2/httpd.conf:
# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Include /private/etc/apache2/vhosts/*.conf
/private/etc/hosts:
127.0.0.1 xyz.localhost
255.255.255.255 broadcasthost
::1 xyz.localhost
/private/etc/apache2/vhosts/xyz.localhost.conf:
<VirtualHost *:80>
DocumentRoot "/Users/MYUSERNAME/dev_projects/MY-REACT-APP/public"
ServerName xyz.localhost
<Directory "/Users/MYUSERNAME/dev_projects/MY-REACT-APP/public">
AllowOverride All
Require all granted
</Directory>
<filesMatch "\.(html|htm|js|css)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
</VirtualHost>
My create-react-app start script:
"scripts": { "start": "HOST=xyz.localhost react-scripts start" }
I found a workaround.
I simply pointed the VirtualHost to the build folder of Create React App, this worked and served me the static files.
Editing the code was pretty tiresome, as I had to run npm run Build after every update.
Luckily, I found the npm watch module. With that, create react App would rebuild on every save. I still have to wait a second between save and refreshing the browser, and it’s absolutely not comparable to using create react app on its own, but it was a good enough solution!

Why is my Netlify-hosted Gatsby site 301 redirecting to a path without a domain?

I did a quick check to see make sure my site was 301 redirecting from:
https://inlunar.com/news/iceye-shows-off-new-sharp-images-from-satellite
to the www. version:
https://www.inlunar.com/news/iceye-shows-off-new-sharp-images-from-satellite
However, when I checked, I found that there was an extra 301 redirect happening from the www. url to:
/news/iceye-shows-off-new-sharp-images-from-satellite
without the domain name anywhere to be found. Here is the full log of that second redirect:
>>> https://www.inlunar.com/news/iceye-shows-off-new-sharp-images-from-satellite
> --------------------------------------------
> 301 Moved Permanently
> --------------------------------------------
Status: 301 Moved Permanently
Code: 301
Cache-Control: public, max-age=0, must-revalidate
Content-Type: text/html; charset=UTF-8
Date: Wed, 17 Jun 2020 01:09:17 GMT
Etag: "8af6153ff17d129285674adb734ca0e3-ssl"
Strict-Transport-Security: max-age=31536000
Age: 0
Server: Netlify
X-NF-Request-ID: 69351fad-bde6-4674-a9b8-fe017a45ee0c-2118676
Location: /news/iceye-shows-off-new-sharp-images-from-satellite/
Why is this second 301 redirect happening?
Netlify appears to be redirecting to the location of the current request with a trailing slash, consistent with their documentation, in an effort to improve cache hit rates.
As for the omission of the domain, it's simply a relative URL.
Relative URLs are URLs that do not include a scheme or a host. In
order to be understood they must be combined with the URL of the
original request.
Client request for http://www.example.com/blog:
GET /blog HTTP/1.1
Host: www.example.com
Server response:
HTTP/1.1 302 Found
Location: /articles/
The URL of the location is expanded by the client to
http://www.example.com/articles/.

Disable caching of HTML template files on apache2 web server

I'm trying to disable caching of HTML templates in my angular app. It seems the easiest way to achieve this is disabling via Apache. I placed this .htaccess file in my web root:
<FilesMatch "\.(html|htm)$">
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Note "CACHING IS DISABLED ON LOCALHOST"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</IfModule>
</FilesMatch>
and restarted the server:
sudo service apache2 restart
but re-loading my browser with a soft refresh still shows the old, cached files.

Local domain simulation with Apache2 and Yii

I'm trying to convert http://localhost/website to http://website.loc, but I'm not able to do that. Here's what I could do:
I edited /etc/hosts (I'm on Ubuntu) by changing 127.0.0.1 localhost to 127.0.0.1 localhost website.loc and saved changes
I created a new file named website inside /etc/apache2/sites-available with this content:
<virtualhost website.loc>
ServerName website.loc
DocumentRoot /home/myuser/projects/website/
<directory /home/myuser/projects/website/>
AllowOverride all
Options Indexes FollowSymLinks MultiViews
Order allow,deny
allow from all
</directory>
</virtualhost>
And I created a softlink to sites-enabled to enable this. After that, I restarted Apache.
By the way, I am using the Yii framework with any request to / redirected to /index.php, so index.php is not needed in the query.
So, when I write website.loc/ into chrome, it moves me to http://website.loc/site/login (the login index page, that's almost expected even if I was logged in as localhost, because the site url "changed" to website.loc, so the cookies are not shared), but the content is:
Not Found
The requested URL /website/index.php was not found on this server.
Apache/2.2.16 (Ubuntu) Server at website.loc Port 80
Am I doing something wrong? Thanks in advance, mates
Edit: It was all about the .htaccess inside /home/myuser/projects/website. It's RewriteBase was pointing to /website. Changing this to / and it worked like charm. Thanks #Chux for reminding me to check the .htaccess!
First, http://website.loc/index.php, check if that work. If that work, means that you need to create an .htaccess in your website root folder to enable that route format

Resources