.htaccess equivalent in KTOR - ktor

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 ktor server, but do not know how?
Any help. thanks.

I think the best way is to add an interceptor to application and apply these rules directly:
intercept(ApplicationCallPipeline.Infrastructure) {
// If call matches conditions
if (call.request.path().endsWith(".mp4") && call.request.headers["Referrer"] != "http://sample.com/video.html") {
// respond with 400 Bad Request
call.respond(HttpStatusCode.BadRequest)
// and stop processing further
finish()
}
}

Related

How to change a apache rewrite rule to an irule

On apache we have the following RewriteCond to block url's with a word in it.
<br/>
RewriteCond %{QUERY_STRING} guest [NC]<br/>
RewriteRule .* - [F]<br/><br/>
How can we transfer this condition to an irule so we can put this on the F5 Loadbalancer?
This will also create a log for each time the event occurs.
when HTTP_REQUEST {
if {([HTTP::uri] contains "guest")} {
log local0.alert "Rejecting \"[HTTP::host][HTTP::uri]\"guest request from [IP::client_addr]"
reject
}
}

Redirection by querying database value like stackoverflow.com URLs

So I use the following htaccess code to redirect the URLs to clean looking URLs but, now what I need to happen is to have the original URLs to redirect to the new clean URLS.
Example
Original URL:
example.com/search/store_info.php?store=113&dentist=Dr.%20John%20Doe
Clean URL:
example.com/search/113/dr-john-doe
What I need is the "ORIGINAL URL" to redirect to the "CLEAN URL". Reason why I need this to happen is that both URLs are showing up in Google searches.
I only want the clean URL to show up and anytime the original URL is used it should automatically redirect to the clean URL. It currently doesn't do that.
Here is my htaccess file.
ErrorDocument 404 default
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /search/
RewriteCond %{QUERY_STRING} .
RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ [NC]
RewriteRule ^ - [L]
RewriteRule ^([a-z0-9]+)/([^\s.]*)[.\s]+(.*)$ $1/$2-$3 [NC,DPI,E=DONE:1]
RewriteCond %{ENV:DONE} =1
RewriteRule ^([0-9a-z]+)/([^\s.]+)$ $1/$2 [R=301,NE,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9-]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]
</IfModule>
I've read about RedirectMatch however I don't know where to implement it into my htaccess file.
Inside your store_info.php have code like this:
<?php
$store = $_GET['store'];
if (empty($store)) {
header("HTTP/1.1 404 Not Found");
exit;
}
$dentist = $_GET['dentist']);
// pull dentist from DB by store id. Create this function in PHP and
// query your database
$dentistFromDB = preg_replace('/[\h.]+/', '-', $row['businessName']);
// DB returns an empty or no value
if (empty($dentistFromDB)) {
header("HTTP/1.1 404 Not Found");
exit;
}
// DB returned value doesn't match the value in URL
if ($dentistFromDB != $dentist) {
// redirect with 301 to correct /<store>/<dentist> page
header ('HTTP/1.1 301 Moved Permanently');
header('Location: /' . $store . '/' . $dentistFromDB);
exit;
}
// rest of the PHP code should come now
?>
You do not need to clutter your htaccess file anymore with arbitrary rules/directives. The current set of rules, with the %{THE_REQUEST} matching, and later with the %{ENV=DONE} conditionals are more than enough.
Just wait a few days for google to crawl your pages again, and the older unclean urls will disappear. You could also search around in Google Webmaster Tools to see if this can be triggered manually.
PS: You can remove the RewriteCond %{QUERY_STRING} . line from your first rule.

Htaccess Block Direct access php file

I have added htaccess rules to rewrite url like following...
RewriteRule ^demo/$ demo.php [L,QSA]
This is working fine for me.
Now issue is i want to block url for demo.php file. Like user can access only
www.example.com/demo/ (valid)
www.example.com/demo.php (not valid and want to block this url)
Please help me do this.
Thanks,
You can use this rule:
RewriteCond %{THE_REQUEST} /demo\.php [NC]
RewriteRule ^ - [L,R=404]

Redirecting Dynamic Url to Static Url

I have read a lot of suggestion, article but i could not fixed my problem. Here is my Url :
us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-education
ja/resources?id=366:video-clip
I want to redirect this URL to static URL. I have tried with .htaccess Like this :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI}
(/|.php|.html|.htm|.feed|.pdf|.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
Redirect 301
/tw/en/investment-opportunities/ultimate-banking-how-to-create-your-own-bank
http://takeshiyashima.com/tw/investment-opportunities/ultimate-banking-how-to-create-your-own-bank
Above URL Fixed and working now but the top URL i have mentioned are not working. I need help.
Please, give a more clear example and post the right link, as it is not clear if there is any symbol before 'ja/resources' and if it's a part of the link at all.
As I understand you want to take only the part:
'my-thought-of-the-day-problem-of-traditional-education'
and redirect to: 'http://takeshiyashima.com/tw/investment-opportunities/my-thought-of-the-day-problem-of-traditional-education'
If it's true here is the possible rule:
us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-education
RewriteRule ^\/?us/my-blog\?catid=\d+id=\d+\:(.*) http://takeshiyashima.com/tw/investment-opportunities/$1 [R=301,L]
Not tested and can be made shorter. If you give more details to the example I will try to help.
Update according to your comment
For ja/resources?id=366:video-clip to takeshiyashima.com/ja/resources it is:
RewriteRule ^\/?ja/resources.* http://takeshiyashima.com/ja/resources [R=301,L]
For 'us/my-blog?catid=1&id=404:my-thought-of-the-day-problem-of-traditional-educatio‌​n', everything is clear, but I see no logic in '/1-my-thought', we have to have a clear pattern. If you say that
you want to take always first 2 words from 'my-thought-of-the-day-problem-of-traditional-educatio‌​n'
then we can make '/1-my-thought', but if you will have different strings like 'my-stuff-education-is-cool', then we will not be able to apply a pattern using regular expressions. Please, provide more original links (3+) you would like to change, so I can see if there is any pattern.
If there is no pattern at all it is possible to use, for example, RewriteMap for Apache with an additional script on the server side, which will handle your links, but it's a more complex way, so let's stick to regular rewrites for now. Waiting for 3+ more examples.

Apache Rewrite Issue, Custom URI

I have a solution but it is one that I know is not the greatest and would better be solved with a full rewrite. Basically I have 3 rewrites that will go to the correct areas I want and do what they need to do. However in order to switch between where I need to go I had to write a URI class to strip through the url set the page and vars manually. It all works out great but the urls are a pain in the ass specially if not formatted exactly.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/bsiadmin/$ /bsiadmin/index.php [L,QSA]
RewriteRule ^/bsiadmin/(.+)/$ /bsiadmin/index.php?page=$1 [L,QSA]
RewriteRule ^/(.+)/$ /index.php?page=$1 [QSA]
So the first rule will make sure to direct everything to the directory and not the root index.php, the second rule does the same if there is a "page" specified. The last rule will take anything else and make sure it uses the root index.php and goes from there.
Example of urls:
http://mysite.test/icecream/id=2/
My custom uri class would strip this clean and set id as a $_REQUEST var.
I guess what I really want to know is how can I just rewrite a simple url such as:
http://mysite.test?page=icecream&id=2
AS
http://mysite.test/icecream/id/2/
Without any limitation on how many vars can be passed and the directory that does exist "bsiadmin" to display without me having to use a uri class to direct it.
Thanks for the help.
You can use mod_rewrite to do so:
RewriteRule ^/([^/]+)/([^/]+)/([^/]+)/(.*) /$1/$4?$2=$3 [N,QSA]
RewriteRule ^/([^/]+)/$ /bsiadmin/index.php?page=$1 [L,QSA]
But I think the best would be to use PHP for that job. Because with mod_rewrite you can only rewrite a fixed amount of URL arguments at a time (here one with every rewrite). With PHP you can parse any arbitrary number of arguments like this:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);
if (count($segments) > 2) {
for ($i=4, $n=count($segments); $i<$n; $i+=2) {
$_GET[rawurledecode($segments[$i-1])] = rawurldecode($segments[$i]);
}
$_GET['page'] = rawurldecode($segments[1]);
}
Then all you need for mod_rewrite is this single rule to rewrite the requests to your index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^/bsiadmin/index\.php$ /bsiadmin/index.php [L]