How do I convert the following Nginx rule into .htaccess?
location ~* \.(jpg|jpeg|gif|png)$ { try_files $uri/ /image-proxy.php$is_args$args; }
Related
I have transferred my server apache to Nginx but I face problem on our .htaccess rewrite rule
I have this rule on .htaccess
RewriteEngine On
RewriteRule ^dl/(\w+)/(\w+)/(.*)/?$ download.php?pt=$1&tk=$2&flen=$3 [L,QSA,NC]
I try this code in Nginx but not working
http{
server {
listen 80;
server_name *.oursitename.com;
location / {
rewrite ^dl/(\w+)/(\w+)/(.*)/?$ download.php?pt=$1&tk=$2&flen=$3 last;
}
}
}
I put location like location / because our download.php file in public_html main area.
please tell me how to it is working fine help me thanks
With the following rewriter:
server {
server_name example.com;
rewrite ^/dl/(\w+)/(\w+)/(.*)/?$ /download.php?pt=$1&tk=$2&flen=$3 last;
}
I have an issue with a Wordpress installation on a subdomain and seems to be redirecting to the root website. When first accessing the subdomain (blog.example.com), it works fine but the second time, it seems to redirect to the root URL (example.com).
Here is the Nginx code:
server {
listen 80;
#listen [::]:80 default_server ipv6only=on;
root /var/www/html/blog-au;
index index.php index.html index.htm;
server_name blog.example.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Please do let me know if you need anymore information from me.
Thanks in advance.
I am having problems creating a rewrite rule that match my .htaccess on Nginx, I am hoping someone can point me to the right direction.
My .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt|app|assets|upload|api)
RewriteRule ^([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) index.php?class=$1&action=$2¶m=$3 [L]
My nginx configuration
location / {
rewrite ^/([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) /index.php?class=$1&action=$2¶m=$3 break;
}
Query params are optional for example:
site.com/
site.com/customers
site.com/customers/add
site.com/customers/edit/1
I cannot rewrite the app code since iam only migrating from apache to nginx.
Any help will be appreciated.
Thanks.
You could structure the solution like this:
root /path/to/root;
index index.php;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/([^/]+)(?:/([^/]+)|)(?:/([^/]+)|)(?:/([^/]+)|) /index.php?class=$1&action=$2¶m=$3 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
# fastcgi stuff
}
I have not checked your regular expression. The try_files directive is documented here.
I'm currently using a rewrite to accept requests for files without including the .php extension, however, I can't find out how to have the requests with .php either be denied or redirected to the friendly version.
For example, this is what I want to accomplish:
/contact.php REDIRECT /contact
/contact.php (/contact.php exists, but only accessible via /contact) would result in a 403 error
Current config:
location / {
try_files $uri $uri/ #extensionless-php;
index index.php;
}
location ~ .php {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Provided that what you have already works, I think what you're looking for is this part:
if ($request_uri ~ ^/(.*)\.php$) { return 301 /$1; }
Or, if you're using query parameters, too:
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 301 /$1?$args; }
You should place it within your \.php$ location (which you should rename from your present .php). Trust me, it's NOT going to generate an infinite loop!
Some more explanation was provided in https://serverfault.com/questions/568569/nginx-url-rewrite-rule/568902#568902.
I want to redirect my domain example.com to www.example.com
My /etc/nginx/sites-available/example.com configuration is
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
What changes should I do in this code?
I have added this line :
try_files $uri $uri/ /index.php?q=$request_uri;
for wordpress permanent link correction.
You need a separate server block to listen for the non www and 301 it to www like this
server {
listen 80;
server_name netmark.com;
return 301 $scheme://www.netmark.com$request_uri;
}
You can read this tutorial to move a domain to another domain but from what I understand you only want to change the actual domain prefix.