To allow video to be played in element from my website, but not allowing it through direct link, I created .htaccess in a sub-directory with the source videos, and nothing else. with the below code:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(mp4|mp3|avi)$ [NC]
RewriteCond %{HTTP_REFERER} !^http://sample.com/.*$ [NC]
RewriteRule ^.* - [F,L]
I need to do the same with .NetCore server, but do not know how?
Any help. thanks.
I think a simple approach to this problem would be an application level solution using .NET Core by simply denying requests based on referrer in your controller by checking referrer header.
If you run .NET Core application(s) using IIS, then you could use IIS rewrite rules specifying them in web.config, and this approach would look similar to .htaccess one. Several examples.
The application level solution might be a little bit slower, however has better visibility to developers and is easier to maintain.
Related
I can do this with mod_rewrite?
Example 1
From Url shop.domain.com/dir/dir1/Products/skuproduct123456 to www.domain.com/products/skuproduct123456
Example 2
From Url shop.domain.com/dir/dir1/pages/namepage/123456 to www.domain.com/collections/namepage-123456
Thank You
What you ask sounds pretty straight forward. You just have to adapt any of the many existing examples you could have found here...
There are two alternatives, however:
First the usual thing is to externally redirect such requests:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/Products/skuproduct123456 https://www.example.com/products/skuproduct123456 [R=301,L]
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/pages/namepage/123456 https://www.example.com/collections/namepage-123456 [R=301,L]
It is a good diea to start out with a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected.
Then it could also be that you actually want to internally rewrite those requests... That is a bit more complex. If both hosts are served by the same http server things are easy again, assuming that both hosts share the same DOCUMENT_ROOT (otherwise you need to adapt the paths accordingly):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/Products/skuproduct123456 /products/skuproduct123456 [END]
RewriteCond %{HTTP_HOST} ^shop\.example\.com$
RewriteRule ^/?dir/dir1/pages/namepage/123456 /collections/namepage-123456 [END]
If however both hosts are served by separate http servers, the only option you have is to use the proxy module. That is also possible from within the rewriting module. This comes with a massive performance penalty though because of how network routing is setup in that case...
All approaches can be implemented not only for static, literal paths you implement but also in a more generic way using regular expressions as patterns. The exact rule depends on the actual structure of your URLs in that case, which you did not really name.
In general it is a good idea to implement such rules in the actual http server's host configuration. You can also use a distributed configuration file (often named ".htaccess"), but that comes with disadvantages. It is only offered as a last means for situations where you do not have control over the http server configuration (read: cheap web hosting providers).
I have problem with RewriteRule. I'm doing english version of website and RewriteRule doesn't seems to work:
RewriteRule ^news/([0-9]+)/.* news.php?id=$1 [NC,QSA]
RewriteRule ^news newslist.php [NC,QSA]
It is weird because previouse version of this code works perfectly. Here it is:
RewriteRule ^aktualnosci/([0-9]+)/.* czytaj.php?id=$1 [NC,QSA]
RewriteRule ^aktualnosci aktualnosci.php [NC,QSA]
What could be wrong with first code? The first rule redirect me to newslist.php rule.
The rules you posted are somewhat exotic... I dare say that probably you are looking for something like that:
RewriteEngine on
RewriteRule ^/?news/([0-9]+)/?$ news.php?id=$1 [NC,L]
RewriteRule ^/?news/?$ newslist.php [NC,L]
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I am archiving a dynamic site by localising it into a static version and hosting it in a subfolder of a new domain.
http://oldsite.com/main.asp to
http://newsite.com/v4/main.asp.html
There was plenty of documentation explaining how to make a redirect:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^oldsite\.com$
RewriteRule (.*) https://newsite.com/v4/$1 [R=301,L]
But I'm struggling to work out the grep for adding .html to any file ending in .asp.
If the code you posted works except for the issue with the filename extension, then you can simply modify it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example.org\.org$
RewriteRule ^(.*)\.asp$ https://example.org/v4/$1.html [R=301,NC,L]
This obviously does not handle any query parameters. But that won't work anyway when turning a dynamic site into a static one as you try, so I assume that simply does not matter...
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I have found some very useful code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
from another Stack Overflow post:
Remove WWW prefix from your website.
I often re-use the same code on various sub-domains and sites so decided to rewrite a generalised version. I came up with the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
It appears to work, but can anyone give me a reason why I should not use this? Have I missed an obvious flaw?
Edit: I am aware that removing the www has associated issues, but in this case I am more interested in problems arising from writing a generalised htaccess RewriteRule (rather than the actions of the rule itself)
Edit: Code edited as recommended by #ulrich
I would also escape the. otherwise it would match www2.somedomain.com and your %1 would end up being .somedomain.com (if you had such a subdomain configured in the future)
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
If you want to further limit it to just a .com you could use
RewriteCond %{HTTP_HOST} ^www\.(.+\.com)$ [NC]
or .net etc
RewriteCond %{HTTP_HOST} ^www\.(.+\.(com|net|edu))$ [NC]
You may also want to add a RewriteBase / after the RewriteEngine On in case you add other rules to your .htaccess later that need it.
Barring those cases, it should work as is.
The answer lies in this word:
optimization
Here's a full explanation:
http://developer.yahoo.com/performance/rules.html#cookie_free
Quote:
When the browser makes a request for a static image and sends cookies
together with the request, the server doesn't have any use for those
cookies. So they only create network traffic for no good reason. You
should make sure static components are requested with cookie-free
requests. Create a subdomain and host all your static components
there.
If your domain is www.example.org, you can host your static components
on static.example.org. However, if you've already set cookies on the
top-level domain example.org as opposed to www.example.org, then all
the requests to static.example.org will include those cookies. In this
case, you can buy a whole new domain, host your static components
there, and keep this domain cookie-free. Yahoo! uses yimg.com, YouTube
uses ytimg.com, Amazon uses images-amazon.com and so on.
The huge problem lies here: read carefully in bold:
if you don't have your www, all the cookies of your the domain will be sent to all the subdomains. I.e. your have 34 small images in your static.mondomain.com and your website is www.mondomain.com, you have 4 kb of cookies. No problem, all is fine. But if your website is mondomain.com without "www" then all those 4kb cookies will be sent to static.mondomain.com when downloading images, creating a huge and useless loss of bandwidth.
That's what I did when I wrote my version #1, then #2 of my framework. Hopefully my version#3 doesn't do this beginner's mistake, and I hope you'll avoid this too!
Long make short
I'm sorry my English is not very good. What I'm trying to say is: if you plan to have a domain name that does everything (web + images + pdf +... and son on) then use *www*. Otherwise don't use it. The yahoo link explains it better than me.
I'm hoping someone can help me. For my website I have a corresponding mobile site that has the same content as my full site but display it for mobile devices. Basically I want to send all requests from the full site to the mobile site unless the url variable sms exists
So in my htaccess file for my full site I have this:
RewriteCond %{QUERY_STRING} !sms=1 [NC]
RewriteRule ^(.*) http://mobile.mysite.co.uk/$1 [QSA,NC]
But when I got to www.mysite.co.uk/news/index.cfm&sms I get the following ColdFusion error for the full site:
File not found: /news/index.cfm
With debugging turned on I've noticed that the CGI variable PATH_TRANSLATED has been changed from
C:\webistes\mysite\news\index.cfm
To
C:\JRun4\bin\http:\mobile.mysite.co.uk\news\index.cfm
I'm at a loss to undestand what's going on? Any help or insight would be greatly appreciated.
Additionally I'm running a multi server install of ColdFusion 8 and using Apache configured for ColdFusion.
It seems like you want to make an external redirection, but your RewriteRule currently only rewrites the URL internally. Try adding the R and L flags to your rule to see if that makes a difference:
# Stop and redirect immediately to the mobile site
RewriteCond %{QUERY_STRING} !sms=1 [NC]
RewriteCond %{HTTP_HOST} !^mobile
RewriteRule ^(.*) http://mobile.mysite.co.uk/$1 [QSA,NC,R,L]
I also added in a RewriteCond to make sure that it doesn't redirect you if you're already on the mobile site, in the event that both of your sites point to the same place (you can remove it if they don't; just wanted to save you the headache in the event that they did).