Can you use htaccess to change from https:// to coui:// - .htaccess

I am writing code to interface with Microsoft Flight Sim and create an in-game display of my website.
MSFS uses javascript to do this and I can load my site in an iframe. I've come across a problem when I try to send messages to/from the page with postMessage and addEventListener. The error message basically states it blocked the frame with coui://html_ui with a frame with http://localhost. See picture...
I believe this might have to do with the answer in this question Blocked iframe due to mismatch protocols
Is it possible to add something to htaccess to rename the http part to coui?? I know we have rewrite rules but not sure if this is possible or where I'd start? I believe that rewriteRule won't allow custom protocols? Can I use
This is my current .htaccess
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /flightcaseV2/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
I've found this answer here How to handle mod-rewrite with a custom url scheme? which might help me? Will RedirectMatch /(.*) myurl://$1 help me in my case?
Alternatively is there a way to send messages to/from the page without triggering this issue?

Related

How do I redirect to all pages to be a subdirectory of another website but keep the URLs the same?

I have a website that has been built at say montypython.netlify.app
The client has their main website at holygrail.com and they want holygrail.com/resources to show the contents of montypython.netlify.app but keep the URL the same. Which means that it should continue to show holygrail.com/resources in the search bar.
This also means that any pages from montypython.netlify.app should appear are subdirectories of holygrail.com/resources
Example:
montypython.netlify.app/about should appear as holygrail.com/resources/about
I am guessing this has to do with editing the .htaccess at holygrail.com but what rewrite/redirect rules can I reference? There are a lot of URLs so is there a wildcard approach I can use?
This is what I've tried:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^holygrail\.com$ [NC]
RewriteRule ^resources/(.*)$ https://montypython.netlify.app/$1 [R=301,L]
</IfModule>
You can use the [P] proxy flag of mod_rewrite.
Using [P] flag instructs mod_rewrite to handle the request via mod_proxy. Therefore, you must enable mod_proxy to use flag.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^holygrail\.com$ [NC]
RewriteRule ^resources/(.*)$ https://montypython.netlify.app/$1 [P]
</IfModule>
with this code snippet, all pages to be a subdirectory resources will be served from https://montypython.netlify.app/ without a 301 redirection.
Maybe your issue is with the line:
RewriteCond %{HTTP_HOST} ^holygrail\.com$ [NC]
Maybe you need to try to do something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^resources/(.*)$ https://montypython.netlify.app/$1 [R=301,L]
</IfModule>
I have worked on projects in the past with similar objectives. I don't believe you can accomplish this with redirects. The suggestion about using a reverse proxy would be the most well aligned with your requirements, but there is another option that may also be useful. Some DNS providers offer "DNS Cloaking" or "Stealth Redirects". This can be configured so that requests for holygrail.com will display a frame containing the content for montypython.netlify.app. Could you use the same approach for the /resources sub-directory, so that holygrail.com/resources delivers a frame that loads montypython.netlify.app?
The drawback to this is the address bar will not change as you navigate inside the frame, e.g. navigating to montypython.netlify.app/resources/about will still show holygrail.com/resources in the address bar, because it is displaying the address of the frame.

.htaccess cans and can'ts

I am very new to the idea of .htaccess and thought that it was what you used to do something like turn this:
http://www.domain.com/some/ugly/url/here.html
into this:
http://www.domain.com/niceurl
I was just told by my ISP that in order to get that to happen, no, it's done by putting the document into the web root folder. That .htaccess isn't used at all.
Does anyone know if this is true? I see a lot of examples about what .htaccess DOES but not so much about what it can't do. Somehow I thought this was all that was needed.
Lastly, if someone types in www.domain.com/niceurl what will happen? Don't I need to have that linked (if not by htaccess, how?!) to the location of the actual file?
Thank you for any and all help. I realize that .htaccess questions abound but they're hard to pick through for the layperson and I'm hoping to answer this specific question.
Here's what I believe should be an answer you want, put the block below to your .htaccess
Answer:
## Enabling Apache's Mod_rewrite module.
RewriteEngine On
# Following line is required if your webserver's URL is not directly related to physical file paths (just / for root, e.g. www.domain.com/)
RewriteBase /
# Restricts rewriting URLs only to paths that do not actually exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Redirect www.domain.com/bar to www.domain.com/foo
Redirect 301 /bar /foo
# Internally load the long URL without changing URL in address bar
RewriteRule ^foo/?$ http://www.domain.com/some/ugly/long/thing/here.html [L,NC]
As a result, www.domain.com/bar will be redirected to www.domain.com/foo and /foo will internally load http://www.domain.com/some/ugly/long/thing/here.html
FYI:
Your website's URL doesn't have to be directly related to physical file paths. Your URL's segment can be served as alias to your URL's parameters. for e.g,
http://www.domain.com/index.php?key1=value1&key2=value2
can be represented as
http://www.domain.com/value1/value2
Note: you need to implement a server side script to be served as a
router to manipulate the URL segments.
For more information about using .htaccess, check this out
Ref: http://htaccess-guide.com/
.htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.
Below is a few examples,
# Custom Error Pages for Better SEO,
# for e.g, to handle 404 file not found error
ErrorDocument 404 http://www.domain.com/404page.html
# Deny visitors by IP address
order allow,deny
deny from 122.248.102.86
deny from 188.40.112.210
allow from all
# Redirects
Redirect 302 /en/my-dir/my-page.html /en/my-path/example.html
# Disallow some silly bots from crawling your sites
RewriteCond %{HTTP_USER_AGENT} (?i)^.*(BlackWidow|Bot\\ mailto:craftbot#yahoo.com|ChinaClaw|Custo|DISCo|Download\\ Demon|eCatch|EirGrabber|EmailSiphon|EmailWolf|Express\\ WebPictures|ExtractorPro|EyeNetIE|FlashGet|GetRight|GetWeb!|Go!Zilla|Go-Ahead-Got-It|GrabNet).*$
RewriteRule .* - [R=403,L]
# Setting server timezone
SetEnv TZ America/Los_Angeles
# trailing slash enforcement,
# e.g, http://www.domain.com/niceurl to http://www.domain.com/niceurl/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301]
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^niceurl/?$ some/ugly/url/here.html [L,NC]
This will allow you to use http://domain.com/niceurl in your browser and it will internally load http://domain.com/some/ugly/url/here.html without changing URL in browser.
If you also want to force redirection from ugly URL to pretty URL then add this redirect rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} \s/+some/ugly/url/here\.html [NC]
RewriteRule ^ /niceurl [R=302,L,NE]

Tricky: Remap url but stay on the same url internally

Need your help. Just spend many ours on this htaccess problem and still don't have a clue how to manage this.
I have many http://www.example.com/menu-alias/foo links on my company's website which should get redirected to http://www.example.com/foo.
This alone shouldn't be the hard part but listen up... the tricky part follows.
I don't manage to get the site (Joomla 1.5) working without the 'menu-alias' this means that all http://www.example.com/foo should get internally mapped to http://www.example.com/menu-alias/foo. So that the user still has http://www.example.com/foo in his browser's address bar.
To make it even more complicated i have to 301 redirect the old menu-alias/foo links to /foo.
Can some htaccess guru help me out? Is this even possible?
You can try adding these rules in the htaccess file in your document root (or vhost config):
RewriteEngine On
# externally redirect requests that have "menu-alias"
RewriteCond %{THE_REQUEST} /menu-alias/([^\ \?]+)
RewriteRule ^ /%1 [L,R=301]
# internally rewrite requests back to menu-alias
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/menu-alias/
RewriteRule ^/?(.*)$ /menu-alias/$1 [L]
Couple of potential problems:
Joomla may be looking for the original un-rewritten request in $_SERVER, if so, the rewrite won't work.
The rule to add the /menu-alias/ back into the URI does so blindly rewrites all requests that don't point to an existing resource. This means "virtual" paths that Joomla may handle will get a "menu-alias" appended to the front.

.htaccess to fake subdomain

I have a requirement where in I want to give users of my site their personal url.
Something like "http://abc.example.com" and when any user types this url in browser it should open this link "http://www.example.com/index/sub-domain?username=abc"
So I tried writing and trying many codes and finally was successful with below code but problem is it redirects. I want an internal redirection. URL address window should remain as "http://abc.example.com".
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*$ index.php [NC]
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example.com$
RewriteRule ^(.*)$ http://www.example.com/index/sub-domain?username=%1
</IfModule>
I am not sure if It is possible or not ? Any advice or help will be of great help. Also can anyone suggest me some .htaccess tutorial.
If you do not specificy the R flag on your RewriteRule, mod_rewrite normally performs an internal rewrite. However, since you are using an absolute URL as your rewrite target, it has to be the exact same host, otherwise an external redirect will be issued.
If you really wish to internally redirect to another host, you should check out mod_proxy.

How do I redirect all but one url to a script

I'm trying to get www.example.com and www.example.com/index.html to go to index.html, but I want all other urls e.g. www.example.com/this/is/another/link to still show www.example.com/this/is/another/link but be processed by a generic script. I've tried
RewriteEngine on
RewriteCond %{REQUEST_URI} !^index\.html$
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.*)$ mygenericscript.php [L]
but it wont work, can someone please help?
Instead of testing what %{REQUEST_URI} is, you can instead just test if the resource exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* mygenericscript.php
This prevents your static resources (images, stylesheets, etc.) from being redirected if they're handled through the same directory your .htaccess is in as well.
What's probably happening now is that you're seeing an internal server error, caused by an infinite internal redirection loop when you try to access anything that isn't / or /index.html. This is because .* matches every request, and after you rewrite to mygenericscript.php the first time, the rule set is reprocessed (because of how mod_rewrite works in the context that you're using it in).
The easiest to do this is to install a 404-handler which gets executed when the server does not find a file to display.
ErrorDocument 404 /mygenericscript.php
or
ErrorDocument 404 /cgi-bin/handler.cgi
or similar should do the trick.
It is not that RewriteRule's can not be used for this, it is just that they are tricky to set up and requires in depth knowledge on how apache handles requests. It is a bit of a black art.
It appears as if you're using PHP, and you can use auto_x_file (x is either append or prepend:
http://php.net/manual/en/ini.core.php

Resources