Redirect a webpage to some other webpage using .htaccess beased on User Agent - html

I'm making an Android app (can't publish it to Play Store because my parents won't let me pay the developer fee) and so, I want the users to be able to check for app updates from within the app.
I have thought of my own way for that because I wasn't able to implement the other ways I found on the internet.
Now, I have a version checking page: "https://app.brokenhearts.ml/check-update.html".
Also, there's an download update page: "https://app.brokenhearts.ml/updated.html".
I've set a custom User Agent to the app "Broken Hearts/1.0". That's working fine as of now, as when websites are loaded from the app they're displaying that custom user agent. I'll be updating the User Agent of the app with every version (for example, app version 2.0 will have the User Agent "Broken Hearts/2.0").
So, whenever a user will check for update, the app is going to load "https://app.brokenhearts.ml/check-update.html" in the webview (with the User Agent "Broken Hearts/1.0"). Now when that's the latest update, I won't have added any special .htaccess rules to redirect it and so, the app will normally load "https://app.brokenhearts.ml/check-update.html". But, suppose I release v2.0, the users still on v1.0, will still be having their user agents set to "Broken Hearts/1.0", and thus, when checking for the update, they shall be redirected to "https://app.brokenhearts.ml/updated.html".
So, basically, whenever I release an update to the app, I'll be updating its User Agent and changing the .htaccess to redirect the users from the old user agent to the "download update page". So, users from any other user agent should remain unaffected and should be able to load the "version checking page" normally.
All of this is going to be based on User Agent and .htaccess. I think it's possible, however, I'm just not able to make the correct .htaccess code.
EDIT: I tried to use this code, I kind of found from Apache docs and modified it as per my needs:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
RewriteRule "^/check-version\.html" /updated.html [R=301,]
However, it gives HTTP error 500.

Well, I found a solution!
I created a folder called "update" and placed the .htaccess in it. Modified it as folows:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]
RewriteRule ^$ https://app.brokenhearts.ml/check-version.html [L]
And I pointed the update checker in the app to https://app.brokenhearts.ml/update/.
So, now, if the user agent matches my custom one, it's getting redirected correctly to updated and if it's the latest one, it's getting redirected to the check version as expected.
Now, whenever I'll publish an update, I'll have to modify the .htaccess when the new rule as follows:
Suoopse, I've posted v3.0 update and users are still using 1.0 and 2.0, then my new .htaccess will be:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/1\.0)$"
RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]
RewriteCond %{HTTP_USER_AGENT} "^(Broken Hearts/2\.0)$"
RewriteRule ^$ https://app.brokenhearts.ml/updated.html [R,L]
RewriteRule ^$ https://app.brokenhearts.ml/check-version.html [L]
I have tried it and it works. Posted it just hoping some other day it might help someone else.

Related

How to access folder files located from other domain folder located inside the public-html?

I want to access the image folder which is located in (e.g "example.com/images/one.jpg").
I have created the subdomain named (e.g "subdomain.example.com").
Both domain folders are in same public_html.
How i can authoize my subdomain, which can access files using htaccess rules or anything else. i tired this "../../" before file name, but not working.
Note: i dont want to put a whole path to access the files from root domain, such as https://......
Thanks
Browsers deal in URLs, not the paths on your filesystem.
It sounds like you have configured your server so that the images are only available on example.com and are not available on subdomain.example.com.
You therefore need to tell the browser to access the images from the other hostname.
src="//example.com/images/one.jpg"
If you have a domain.com and sub.domain.com, these are two completely different paths. You cannot get data from each other using reference as path like '../images/foo.png'.
In this case, you have to put all the path https://www.sub.domain.com/images/foo.png. This is the same way you need to do when uses a subdomain as resource in order to decrease the number of request in your website for images and sources in general.
I did a little bit of server configuration in the .htaccess file. I write the rule that no image can be publicly accessible. but I want to authorize my subdomain which can access the image URL.
:
Note: both domains are in the same public_html directory
You would seem to require an exception for the subdomain and block other hostnames?
It's a bit of a guess how you are currently doing this - either with mod_authz_core and a <FilesMatch> container perhaps? Or using mod_rewrite? Or an Apache <If> expression??
You could do something like the following using mod_rewrite, near the top of the root .htaccess file to block access to all images, except when accessed via the subdomain.
RewriteEngine On
# Block (403 Forbidden) all image requests unless accessed through the subdomain
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com
RewriteRule \.(jpe?g|png|webp|gif)$ - [F]
Now, a request of the form https://example.com/images/one.jpg will be blocked, but https://subdomain.example.com/images/one.jpg will be accessible.
i dont want to put a whole path to access the files from root domain
But you will need to use an absolute (or protocol-relative) URL to access the files from the subdomain.
UPDATE:
Current rules in my parent domain "example.com"
RewriteCond %{HTTP_REFERER} !^https://example.com/ [NC]
RewriteRule \.(gif|jpg|jpeg|png|mp4|mov|mkv|flv|svg)$ - [F,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Ah, so you've implemented a form of hotlink protection! In that case you would seem to just need to modify the first condition to allow requests from the subdomain as well. For example:
RewriteCond %{HTTP_REFERER} !^https://(subdomain\.)?example.com/
To allow requests from both example.com and subdomain.example.com. The NC flag should be omitted. All browsers will lowercase the hostname in the request.
No other rules are required to "allow" access. You do not need to use the absolute URL to access these resources - since that does not appear to be what you are blocking. You simply need to be able to access these resources from another host (ie. the subdomain).
In summary:
RewriteEngine On
# Hotlink protection
RewriteCond %{HTTP_REFERER} !^https://(subdomain\.)?example.com/
RewriteRule \.(gif|jpg|jpeg|png|mp4|mov|mkv|flv|svg)$ - [F]
# HTTP to HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The L flag is not required with the F flag, since it is implied.
The ^(.*)$ is unnecessary since the backreference is not being used. Simply having ^ is sufficient and more efficient - to be successful for everything without having to actually match everything.

The URL adds /public_html/ after changes in .htaccess

I'm a newby on using the .htaccess file for making changes on my website.
I want to change a URL automatically by adding the "www." at the begining of the domain name. To do so I enter the following code on the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.arnaldpedros.com$ [NC]
RewriteRule ^(.*)$ http://www.arnaldpedros.com/$1 [L,R=301]
I see allover the web that this is the proper way to do it but when I type arnaldpedros.com in my browser it takes me to:
http://www.arnaldpedros.com/public_html/
Whis is a 404 Not Found page, with the message:
Not Found
The requested URL /public_html/ was not found on this server.
What I want to do is to type arnaldpedros.com and to automatically go to www.arnaldpedros.com.
It's driving me crazy cause the same happens on many websites I mange.
Any help please?

Images on my server redirecing to Google?

So I woke up this morning to my Wordpress websites all having their htaccess file modified (redirecting to spam). I replaced them with a backup file:
# 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
And then I refreshed my site and only 5% of the images from my /images/ folder are showing up.
All of my <img src paths are absolute. Even if I go to my image directory and click the image link directly, my browser thinks for a little bit and then goes to: https://www.google.com/?gws_rd=ssl
However, like I said, about 5% of the image links do actually work, and they're all the same filetype in the same directory.
It's very strange.
Edit: It seems to have been a server cache thing, things cleared up on their own after a few more minutes.
First, clear the cache of your browser, or try with a "Private Session" of Chrome / Firefox, maybe redirects are just still in your cache.
Second, check if there is another .htaccess in the images directory or in any parent directory up to the root of your site and check its content.
Third, check if non working images are presents on filesystem.
If there aren't anymore the redirect come from Wordpress and not from Apache.
Your CMS has been defaced, if you have a backup I think it's a good idea to restore it, or at least compare it file by file with your actual site (WinMerge can help you doing this).
I strongly suggest you to restore the backup and then check if your Wordpress installation is up-to-date and so every wordpress plugin you use.

Hiding a SINGLE File (Apahe)

I have a HTML file that is full of IP addresses from a PHP script that logs all IP's of vistiots. The only thing is that I don't want people to be able to go to
https:// mydomain.us/ips.html to see all of the IP's logged. What are my options to hide this ONE file.
I read all of these things with a code you put in your .htaccess, but none of them worked!
All I need is a way to hide this HTML file!
Sorry if this a bad question also, I'm really new to all of this :P
PHP Script:
(Used to write to the HTML File)
http://pastebin.com/jKDHeArb
Final Output (ips.html)
http://imgur.com/VWPmuSt
I suggest you put this page in a folder, and protect it with a password. There are many examples here or on the Internet.
But like you do not really want to block the page, but just to hide, You can do this by adding a code at the end of link. With this root .htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{QUERY_STRING} !^c=pass$ [NC]
RewriteRule ^ips\.html$ - [NC,F,L]
After it's possible to visit the page only with the following https link:
https:// mydomain.us/ips.html?c=pass
You can do the same in the php code.
you can add this to either an .htaccess or your Vhost
<Files ~ "ips.html">
Order allow,deny
Deny from all
</Files>

WordPress JSON API returns only 404 errors

I'm brand new to Wordpress and I would like to use the JSON API plugin.
So I put it in my plugin repository and I activated it but when I try to display the Json response by accessing the http://localhost/wordpress/wp-json/posts url I get a 404 error.
I'm probably missing something but according to the documentation it should be that simple. Any idea of what am I doing wrong?
It is an mod_rewrite issue.
Reason is one of these in your .htaccess:
not existing
wrong permissions
screwed up
Try the htaccess documentation on Wordpress for your .htaccess:
## 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
Old question and answer, but for anyone coming here recently via search results (like me), /wp-json/posts should at least bring a JSON result (albeit still a 404 error), and /wp-json should list some available routes in JSON.
If it doesn't (e.g. it shows an Apache or other 404 error page), it's probably a permalinks issue
You have the wrong address. According to the documentation, the following are valid links:
Implicit mode examples:
http://www.example.org/?json=1
http://www.example.org/?p=47&json=1
http://www.example.org/tag/banana/?json=1
Explicit mode examples:
http://www.example.org/?json=get_recent_posts
http://www.example.org/?json=get_post&post_id=47
http://www.example.org/?json=get_tag_posts&tag_slug=banana
With user-friendly permalinks configured:
http://www.example.org/api/get_recent_posts/
http://www.example.org/api/get_post/?post_id=47
http://www.example.org/api/get_tag_posts/?tag_slug=banana
Source: https://wordpress.org/plugins/json-api/other_notes/
So in your case you should use http://localhost/wordpress/api/get_recent_posts/
/wp-json/elementor/v1/globals - 404 error
Just update the plugin if any update is available and deactivate it first and then re-activate it again. The problem will be solved.
When we activate the Elementor plugin then this plugin creates some essential files for editing, so when we will re-activate it then it will re-create all essential files and our problem will be solved. Thanks
It may depend on whether you have NGinx or Apache.
On some hosts with Plesk if Nginx is used, Plesk does not create or re-create the .htaccess file, but for WP it must be present.
If it's not there, just recreate it with the standard WP settings https://wordpress.org/support/article/htaccess/ as mentioned in one of the previous answers.
A workaround (insecure) is to create directories and files /wp-json/elementor/v1/globals with {} like Json standard, elementor will work the same, but I don't recommend doing that.