How to change a apache rewrite rule to an irule - apache

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
}
}

Related

RewriteCond and rules except some page

Hello I want use redirect module on apache2
if request "test.net/xxx" then redirect to "test.test..net/xxx"
but except index.html page
for example if request "test.net/index.html" then no redirect
i wrote the apache2 conf file as below.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.net [NC]
RewriteRule ^http://test\.net/([^index\.html]) http://test.test.net/$1 [R=301,L]
but doesn't work
how to edit above sentences? please tell me
thanks!
You add a condition to take care that the rule is only applied if the exception is not met and fix the actual rewriting rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteRule ^/?(.*)$ http://test.test.net/$1 [R=301]
That rule will work in the http servers host configuration but also in dynamic configuration files (".htaccess").
If you receive an http status 500 ("internal server error") with above rule then chances are that you operate a very old version of the apachE http server. The error log files will then point out an unknown flag [END]. Replace it with the old [L] flag in that case.

.htaccess equivalent in 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()
}
}

.htaccess url rewriting is working but not the redirection

I'm working on a local server on an html/php application and I'm trying to use the Apache url rewrite module without success
The application was stored in ./Compta/index.php. I have to an .htaccess file in ./Compta/.htaccess
I would like to only use a rewritten url like :http://localhost/Compta/Test/
instead of : http://localhost/Compta/index.php?page=test
and redirect users if they try to go to the old url
The .htaccess file contains :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L]
RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301]
When I go to http://localhost/Compta/Test/ the following line is working and my code includes in a div the content of test.php :
RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L]
When I go to http://localhost/Compta/Test the following line is working but in firefox the url is rewritten to http://localhost/Compta/index.php?page=Test and this is not happening with http://localhost/Compta/Test2; the url isn't rewritten.
RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L]
To fix this and to redirect the old url I added these lines :
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301]
But this is not working and when I go to http://localhost/Compta/index.php?page=Test the url isn't rewritten to http://localhost/Compta/Test/
Thank you in advance
I didn't find a solution with .htaccess but i found one with php so i added the following lines in top of my php files :
if(preg_match("/\/Compta\/index\.php\?page=([[:alnum:]]*)/",#$_SERVER['REQUEST_URI'],$page)&&!empty($page[1]))
{
$new_url = "/Compta/";
switch (strtolower($page[1])) {
case "test":
$new_url = $new_url."Test/";
break;
case "test2":
$new_url = $new_url."Test2/";
break;
default:break;
}
header("Status: 301 Moved Permanently", false, 301);
header("Location: ".$new_url);
exit;
}
I test if the url begin with "/Compta/index.php?page=" AND if there is a parameter for "page"
Then I initialise the variable which contain the new url
Switch the content of my parameter I modify the new url
and then I make the redirection to my new url :)

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.

Subdomain to Folder using Apache mod_rewrite related Problem

I want to map all my subdomain to folder.
For example subdomain.domain.com should display content of domain.com/clients/subdmain (Transparently)
RewriteCond %{HTTP_HOST} ^(.+)\.oomail\.org
RewriteCond %{HTTP_HOST} !^www\.oomail\.org [NC]
RewriteRule ^(.+)$ /var/www/clients/%1/$1
I got 500 Internal Server Error. To check whats the problem I appended [R] flag
RewriteRule ^(.+)$ /var/www/clients/%1/$1 [R]
I got this result in address bar
http://q1.oomail.org/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/always.html
Then I created a details.php file which dumps the $_GET variable details. Its content is
<pre>
<?php
var_dump($_GET);
?>
</pre>
and modified RewriteRule to (Note: I am sending $1 and %1 into details.php which shows its content)
RewriteRule ^(.+)$ details.php?p=%1&d=$1
And after opening http://subdomain.oomail.org/file.html, I got output as
array(2) {
["p"]=>
string(9) "subdomain"
["d"]=>
string(11) "details.php"
}
Of which variable p is correct (value of %1) which is "subdomain", BUT d (value of $1) is details.php which I was expecting to be file.html which I opened in URL. Where am I going wrong? or How can I fetch file.html (URI) into variable for mapping?
Use this rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/clients/
RewriteCond %{HTTP_HOST} ^(.+)\.oomail\.org
RewriteCond %{HTTP_HOST} !^www\.oomail\.org
RewriteRule ^(.*)$ /clients/%1/$1 [L]
In your first attempt you had endless rewrite loop which Apache had to interrupt (by default no more than 10 iterations). In second attempt you are facing limited rewrite loop (2 iterations, I believe). That's how mod_rewrite works and you should count such possibility in your rules -- especially when you use .* as matching pattern .
The very first condition in my rule will prevent rewrite loop from happening (means rule will only work on very first iteration). Unfortunately this means some restrictions: the original URL CANNOT start with /clients/. For example: this URL will not be redirected: http://something.example.com/clients/hello-kitten.php.
Solution -- rename clients folder to something more unique: for example _clients_folder_ (you would have to change that folder in rewrite rules as well, obviously).
Something to read: RewriteRule Last [L] flag not working?