I'm currently in the process of moving from Apache to Nginx and am having problems with one of the rewrite rules. I have the following rule in my old .htaccess:
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
How would I rewrite this rule for Nginx? Everything I try either doesn't work or simply prevents Nginx from reloading.
You can use this rule:
Location ~ \.(css|js)$ {
rewrite "^(.+)\.\d{10}\.(css|js)$" $1.$2;
}
Related
Trying to figure out how to get the following rewrite rule changed to work with Litespeed.
Previously the below rewrite rule worked with Apache, but stopped working with Litespeed.
RewriteRule ^product-page/category-one/ index-redir.php
[L,E=URI:/category-one/]
RewriteRule ^category-one/ /product-page/category-one/
[R=301,L]
I have trouble figuring out how to rewrite this htaccess rule to a nginx rule. It's for a legacy project that uses a static asset version in the url.
The file on the server is for example located in:
assets/js/jquery.min.js
The url to fetch it is:
assets/js/jquery.min.1661243858.js
The htaccess rule is
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?\/)*?([a-zA-Z\.\-]+)(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1$2$4 [L]
</IfModule>
I tried some converters and found a blog post going over this. But it doesn't seem to be working. I'm using ddev as my environment. Current nginx rule that I set in .ddev/nginx_full/nginx-site.conf, I removed #ddev-generated:
location ~* (.+)\.(?:\d+)\.(js|css|png|jpg|jpeg|gif|webp)$ {
try_files $uri $1.$2;
}
So the problem was that I assumed doing ddev start applied the changes made in .ddev/nginx_full/nginx-site.conf. It doesn't. I needed to use ddev restart. The nginx rule is correct.
I need to translate htaccess rewrite rule for a IIS web server, can you help me with the sintax? for example:
RewriteCond %{QUERY_STRING} ^orderby=([0-9]+)&pagesize=([0-9]+)&pagenumber([0-9]+)$
RewriteRule ^[OLD-SLUG]$ https://www.[DOMAIN].it/[NEW-SLUG]? [R=301,L]
Or:
RewriteRule ^[OLD-SLUG]$ - [G]
Thanks
Many PHP applications currently ship with rewrite rules as part of their .htaccess file. These rules tell Apache's mod_rewrite how and when to rewrite incoming requests. The IIS URL Rewrite module can read these rules and translate them into URL Rewrite rules.
For more information about importing Apache mod_rewrite rules, see: Importing Apache mod_rewrite Rules.
I have a site that currently redirects to the public folder from root /
like so...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
RewriteRule ^(.*).php$ /$1 [R=301,L]
I would like to add an exception for a system folder that needs to redirect to a different folder named 000999
RewriteCond %{REQUEST_URI} !/system [NC]
RewriteRule ^system/(.*)$ /000999 [L,NC]
However the exception whether placed after "Rewrite Base" or anywhere else fails to redirect
From your description and the example I understand that if the "folder" system is requested, regardless of the path below, there should be an internal rewrite to the folder /000999? Not a redirection? And the path should be ignored?
This should do, note that I also fixed a couple of other issues with your current setup:
RewriteEngine On
RewriteBase /
RewriteRule ^/?(.*)\.php$ /$1 [R=301]
RewriteRule ^/?system/(.*)$ /000999 [END]
RewriteRule ^/?(.*)$ /public/$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
If my url will like this:
http://id.factor.ua/bez-rubriki/checkout/?price=...
And url will contains bez-rubriki
How I can redirect using .htaccess to this url BUT without this slug, like next URL:
http://id.factor.ua/checkout/?price=...
As simple as this for an internal rewrite:
RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [L,QSA]
For an external redirection add the R flag:
RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]
The above are the versions to be used inside .htaccess style files.
A general note: if you have control over the http server configuration, then you should always prefer to place such rules inside the host configuration instead of using .htaccess style files. Those files are notoriously error prone, make things complex, are hard to debug and really slow the server down. They should only be used if there is no control of the http server configuration or if some app requires dynamic changes to the configuration.
So in case you want to place those rules in the host configuration you need a small modification. You have to include the leading slash (/) into the regex testing the request path:
RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [L,QSA]
And the version doing an external redirection:
RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]