I am trying to set up a Nuxt app using #nuxt/pwa starter-template for Server Side Rendering on a Shared Hosting.
However my Nuxt app is running on "http://localhost:50000". I am trying to show the running app from "http://example.com" by Rewriting Rule in .htaccess.
When trying to access "http://example.com" it's showing 404. Without the "Index (/)" route every other route is fine. Even coming to "Index (/)" route from any other route is also OK. Only showing 404 when trying to load the "/" route directly.
Eg:
http://example.com Doesn't work
http://example.com/contact Works fine
*** There is already an issue exactly like mine at Github: https://github.com/nuxt/nuxt.js/issues/2625
But I couldn't found anything helpful from there.
Here is the .htaccess file I am using. --
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*) http://127.0.0.1:50000/$1 [P,L]
</IfModule>
Can somebody help me out with this issue??? Thanks in advance!
It seems to be a bit late for this answer, but I only found it in github and would like to help anyone who finds this topic with the same problem.
This htaccess is working for me, it loads the index.vue page normally when I refresh and the navigation works.
Replace my-domain with your domain.
RewriteEngine on
# Forcing all incoming requests to HTTPS.
# HTTPS is required for PWA. If you don't want PWA feature you can deisable next 2 lines
RewriteCond %{HTTP_HOST} ^my-domain.com.br$
RewriteRule "(.*.(jpg|gif|png|svg|js|css|woff2))$" "http://127.0.0.1:3000/$1" [P,NC]
RewriteRule ^(.*) "http://127.0.0.1:3000/$2" [P,L]
Use my .htaccess code:
RewriteRule ^index.html.var$ http://127.0.0.1:3000/ [P]
RewriteRule ^(.*) http://127.0.0.1:3000/$1 [P]
This problem has also troubled me for a long time.
I added _index.vue dynamic vue-router and,
validate ({params}) {
console.log (params.index);
}
The terminal outputs "index.html.var".
So I was thinking that when we visited the homepage, Apache sent a proxy, but the request URL received by nuxt.js was not "/" but "index.html.var".
A little late to this party but if anyone is experiencing the same issue, what fixed it for me was to disable DirectoryIndex in .htaccess.
RewriteEngine on
DirectoryIndex disabled
RewriteRule ^(.*) http://127.0.0.1:50000/$1 [P,L]
RewriteEngine on
Forcing all incoming requests to HTTPS.
HTTPS is required for PWA. If you don't want PWA feature you can deisable next 2 lines
RewriteCond %{HTTP_HOST} ^my-domain.com.br$
RewriteRule "(..(jpg|gif|png|svg|js|css|woff2))$" "http://127.0.0.1:3000/$1" [P,NC]
RewriteRule ^(.) "http://127.0.0.1:3000/$2" [P,L]
Work for me.
Related
The homepage/base url of the app can be refreshed with no issues. but other pages return 404 on page refresh. Please are there any work around for this?
Here is a screenshot of the 404.
[1]: https://i.stack.imgur.com/lc8xD.png
It happens because Vue routing is only on the frontend side. So when you refresh the page it goes backend server. And it checks for any files to satisfy the request.
To solve this issue. You have to tell your apache webserver to handle it for your front end Vue app.
Try adding this to your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
The solution that worked for me:
In /etc/apache2/sites-available/"yourAppDomain".conf add to the end:
FallbackResource /index.html
</VirtualHost>
Page refresh returns 404 error VueJs (mode:history) with Laravel:
The solution that worked for Laravel:
Assuming your entry point is the index method, you just need to add this route at the bottom of your routes/web.php file.
Route::get('{any}', function () {
return view('welcome');
})->where('any','.*');
I'm hosting my React app on hostgator which uses Apache. In order to enable refreshing of the page without getting 404 not found I need to create and edit an .htaccess file which looks like this right now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.html [L]
</IfModule>
What happens when I refresh the page on a subpage (/some-sub-page) is the index.html renders but the URL resets to root.com (without the /some-sub-page).
I want the index.html to render, but I also want to keep the url in order for React Router to kick in and serve the correct components.
I've gone through the Apache documentation as well as threads about .htaccess editing but none seems to cover the problem with the URL resetting.
How would one go about that?
Best regards.
I have an e-commerce site that resides in:
http://dev.gworks.mobi/
When a customer clicks on the signin link, the browser gets redirected to another domain, in order for authentication:
http://frock.gworks.mobi:8080/openam/XUI/#login/&goto=http%3A%2F%2Fdev.gworks.mobi%3A80%2Fcustomer%2Faccount%2Flogin%2Freferer%2FaHR0cDovL2Rldi5nd29ya3MubW9iaS8%2C%2F
I'm trying to rewrite http://dev.gworks.mobi/* to http://frock.gworks.mobi:8080/openam/*, without redirection.
I've tried this in the .htaccess of the dev.gworks.mobi site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/openam(.*)$ [NC]
RewriteRule ^(.*)$ http://frock.gworks.mobi:8080/$1 [P,L]
</IfModule>
But when I access http://dev.gworks.mobi/openam, it shows a 404 page not found page.
Can anyone help me to achieve my use case?
Try this:
RewriteEngine on
RewriteBase /
# Make sure it's not an actual file being accessed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match the host
RewriteCond %{HTTP_HOST} ^dev\.gworks\.mobi
# Rewrite the request if it starts with "openam"
RewriteRule ^openam(.*)$ http://frock.gworks.mobi:8080/$1 [L,QSA]
This will rewrite all the requests to dev.gworks.mobi/openam to frock.gworks.mobi:8080.
If you want to mask the URI in a way that it's not visible to the visitor that she's visiting the authentication app, you need to add a P flag. Please note that it needs Apache's mod_proxy module in place:
RewriteRule ^openam(.*)$ http://frock.gworks.mobi:8080/$1 [P,L,QSA]
Feel free to drop the L flag, if it's not the last rewrite rule. See RewriteRule Flags for more information.
The 404
If it's all in place and you're still getting a 404 error, make sure that the target URL is not throwing 404 errors in the first place.
Second, check if you're still getting the error with the correct referrer URI set. It might be designed in a way to throw a 404, if the referrer is not correctly set. If that's the case, which I suspect, you need to use the R flag and redirect instead of proxying the request.
Last thing that comes to my mind, some webapps are not built in a way to figure out the URI address. The host, as well as the port number, might be hard-coded somewhere in the config files. Make sure that the authentication app is able to be run from another URL without the need to edit the configs.
Test
You can test the rewriterule online:
After several hours of trying a myriad of suggestions for .htaccess I have given up and decided to ask here.
I have a single html page that needs to be served via SSL. It is a single file with the .htm extension and it contains no php whatsoever. If anybody accesses this page via typing it in or clicking on a link from a non SSL page, I want that person to be redirected to or shown the SSL version of that page. Only https://example.com/myfile.htm should be allowed. The rest of the site can go without SSL, just this one page needs it.
Please help.
Try this in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !^on$
RewriteRule ^myfile\.htm$ https://www.example.com/myfile.htm [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^myfile\.htm$ https://example.com/myfile.htm [L,R]
SSL for HTML only page / SSL for JavaScript only page:
If anyone needs to set SSL (HTTPS) for page that uses html only (without PHP, nodeJS etc.) just put .htaccess file in the same folder as index.htm page.
Content of the .htaccess have to be:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I had trouble finding how to, so I think that this thread is the best to hold this answer.
I want to force Apache to use HTTPS for a particular URL in the following form:
https://www.example.com/signup/*
so
if someone goes to any of the following example URLs directly, Apache will forward the URL over to the HTTPS equivalent site.
e.g.
http://www.example.com/signup --> https://www.example.com/signup
http://www.example.com/signup/basic+plan --> https://www.example.com/signup/basic+plan
http://www.example.com/signup/premium --> https://www.example.com/signup/premium
Anyone know how?
Thanks in advance
Thank Murat,
Yours almost worked but figured out how to get it to exactly work.
The following is what works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
Notice that I didn't include somefolder in the www.domain.com rewriterule
I think this was what i used:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
(from here)
You can use the Redirect directive:
Redirect 301 /signup https://www.example.com/signup
This will automatically preserve anything following /signup in the URL. Be sure to configure this directive only on your non-SSL site, or it might get into a recursive loop!
You should take a look at mod_rewrite documentation
I used the following to require the checkout section of a website to require SSL:
<Directory "/var/www/html">
RewriteEngine on
Options +FollowSymLinks
Order allow,deny
Allow from all
RewriteCond %{SERVER_PORT} !^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule ^checkout(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>
So for example, hitting http://www.example.com/checkout redirects to https://www.example.com/checkout
The rule will skip file extensions that are typically included within a page so that you don't get mixed content warnings. You should add to this list as necessary.
If you want multiple pages change the RewriteRule to something like:
RewriteRule ^(checkout|login)(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Of course, the directory should match the actual path on your server. This page may also help with some more information for your specific needs: http://www.whoopis.com/howtos/apache-rewrite.html
I'm using this on a website that runs Plesk 8.6 but that shouldn't matter. This is in my vhost.conf file which is like putting it in your httpd.conf file. I'm not sure if you'd need to adjust anything to use it in a .htaccess file but I doubt it. If adding to a conf file don't forget to restart apache to reload the configuration.
If you are like me and want to use SSL only on particular pages then you also want a rewrite rule that sends you back to regular http for the rest. You can use the following for the reverse effect:
RewriteCond %{SERVER_PORT} ^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule !^(checkout|login)(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R]
If you are using Plesk like I am keep in mind that all non-SSL traffic uses the vhost.conf file but all SSL traffic uses the vhost_ssl.conf file. That means your first rewrite rule to require SSL would go in the vhost.conf file but the second rule to force back to non-SSL will have to go in the vhost_ssl file. If you are using httpd.conf or .htaccess I think you can put them both in the same place.
I've also posted this tutorial on my blog: Apache rewrite rules to force secure/non-secure pages.
You can do this with mod_rewrite -
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/signup https://example.com/signup
RewriteRule ^/signup/(.*)$ https://example.com/signup/$1
Should work, though I haven't tested it.
-- edit --
Correction, I just tried this on one of my servers, and it works fine for me. You may want to doublecheck your mod_rewrite configuration. Also, if you're using .htaccess, you'll want to make sure overrides are allowed for that directory.
As a side note, this assumes your SSL traffic is coming over port 443. If it isn't, you'll need to adjust the rewrite condition accordingly.
.htaccess files are normally placed in a scope with Options -FollowSymLinks, which blocks Rewrite rules. This is often a security rule.
So a more trivial thing is often needed like this one:
<If "%{HTTPS} != 'on'">
Redirect 301 /your/path https://www.example.com/your/path
</If>
This is a small enhancement to the answer of Greg Hewgill.