How to convert this apache rewrite rule to lighttpd rewrite rule?
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /adpanel/
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
Try
server.modules = (
"mod_access",
"mod_rewrite"
)
$HTTP["host"] == "example.com" {
server.document-root = "/var/www/example.com/wwwroot/adpanel/"
$HTTP["url"] =~ "^/application" {
url.access-deny = ("")
}
$HTTP["url"] =~ "^/modules" {
url.access-deny = ("")
}
$HTTP["url"] =~ "^/system" {
url.access-deny = ("")
}
## If later than lighttpd 1.4.24
url.rewrite-if-not-file = (
"^/(.*)$" => "/index.php/$1"
)
## If older than 1.4.24 yo will have to reference actual files in the rewrite e.g.
#url.rewrite = (
# "/(css|img|js|stats)/" => "$0", ## $0 means the actual query string i.e don't rewrite.
# "^/(.*)$" => "/index.php/$1"
#)
}
Hope that helps.
Related
htaccess to nginx rewrite conversion help
RewriteEngine On
RewriteCond %{REQUEST_URI} apiv01
RewriteRule ^(.*)$ api.php?params=$1 [NC]
--- edit
I want when access uri GET http://localhost/hospital_project/apiv01/listHospital/3211 can display
{"header":{"code":"401","message":"wrong token"}}
I've used the configuration of anilcetin like this:
location /hospital_project {
if ($uri ~ "apiv01"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/(.*)$ /hospital_project/api.php?params=$1;
}
}
Also i've used the configuration of https://winginx.com/en/htaccess like this:
location / {
rewrite apiv01 /hospital_project/api.php?params=$1;
}
But when i access http://localhost/hospital_project/apiv01/listHospital/3211 the output is equal to http://localhost/hospital_project/apiv01 (no effect)
--- edit
Normal if i using http://localhost/hospital_project/api.php?params=apiv01/listHospital/3211
Could someone help me to convert this rules for nginx? I need it to serve html as xhtml for svg mask in FireFox
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - [T=application/xhtml+xml]
it is more complex
set $var "q";
if ($http_accept ~ application/xhtml\+xml) {
set $var "q$var";
}
if ($http_accept !~ 'application/xhtml\+xml\s*;\s*q=0') {
set $var "q$var";
}
if ($uri ~ \.html$) {
set $var "q$var";
}
if ($server_protocol = HTTP/1.1) {
set $var "q$var";
}
if ($var = qqqqq) {
add_header 'Content-Type' 'application/xhtml+xml';
}
not tested
Please help me convert the following Apache htaccess rules to Nginx
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://192.168.201.112/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://192.168.201.112$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ - [F,NC]
Thanks in advance
This configuration means only 192.168.201.112 can access the resouce which has extsion name (jpg|jpeg|gif|png|bmp|swf) in this server. You can use the following nginx config:
location ~ .*\.(jpg|jpeg|gif|png|bmp|swf)$ {
set $hit false;
if ($http_referer ~ "^http://test1.test.com/.*$"){
set $hit true;
}
if ($http_referer ~ "^http://test1.test.com$"){
set $hit true;
}
if ($hit = false) {
return 403;
}
}
I want to create .htaccess rule for situation like below:
I have a link to file: http://something.com/images/some/image_001.png
If this file doesn't exists I want to redirect to the newest file in /images/some directory
Is something like this possible using .htaccess? I know that I can check if file exists with RewriteCond, but don't know if it is possible to redirect to the newest file.
Rewriting to a CGI script is your only option from a .htaccess, technically you could use a programatic RewriteMap with a RewriteRule in a httpd.conf file.
The script can serve the file directly, so with an internal rewrite the logic can be entirely server side e.g.
.htaccess Rule
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^images/(.*)$ /getLatest.php [L]
Where getLatest.php is something like:
<?php
$dir = "/srv/www/images";
$pattern = '/\.(jpg|jpeg|png|gif)$/';
$newstamp = 0;
$newname = "";
if ($handle = opendir($dir)) {
while (false !== ($fname = readdir($handle))) {
// Eliminate current directory, parent directory
if (preg_match('/^\.{1,2}$/',$fname)) continue;
// Eliminate all but the permitted file types
if (! preg_match($pattern,$fname)) continue;
$timedat = filemtime("$dir/$fname");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fname;
}
}
}
closedir ($handle);
$filepath="$dir/$newname";
$etag = md5_file($filepath);
header("Content-type: image/jpeg");
header('Content-Length: ' . filesize($filepath));
header("Accept-Ranges: bytes");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $newstamp)." GMT");
header("Etag: $etag");
readfile($filepath);
?>
Note: Code partially borrowed from the answers in: PHP: Get the Latest File Addition in a Directory
I have an htaccess file that uses mod_rewrite to redirect /controller to /index.php?controller=%controller%
Like this:
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite current-style URLs of the form 'index.php?controller=x&action=y'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?controller=$1 [L,QSA]
</IfModule>
Now, what I need to be able to do is make ONE of the controllers work with HTTP Authentication. I'm not asking if this is the best way to do things, I'm simply asking how to do it.
Example:
http://www.example.com/ - It requires no auth
http://www.example.com/secret - requires auth
<Location /secret>
AuthName localhost
AuthType Basic
AuthUserFile <file>
Require valid-user
</Location>
I ended up using PHP to do it:
if (in_array($controllerString, $configuration['protected']))
{
$authenticated = false;
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'You are unatuhorized to access this section of the website.';
} else if ($_SERVER['PHP_AUTH_USER'] == 'admin' && $_SERVER['PHP_AUTH_PW'] == 'admin'){
$authenticated = true;
}
if (!$authenticated)
{
unset($_SERVER['PHP_AUTH_USER']);
die();
}
}