Converting htaccess rewrite rule to nginx - .htaccess

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.

Related

How to write .htaccess rewrite rule to solve 404 error when angularjs html5mode is enabled

I am new to AngularJS and PHP. In the application i am working on, I have enabled html5mode in angularjs using following things:-
-- Added base href in /myapp/index.html
<base href="http://localhost/myapp/" />
-- Added following at the end of config function.
$locationProvider.html5Mode(true);
When I access my application using following url: http://localhost/myapp ,
It does not append any # in browser url. I can navigate to any other route successfully. Route url is http://localhost/myapp/category/1
But when I refresh the page, above url gives me 404 error. I understood that, server trying to find file at location /myapp/category/1. I read at other places writing .htaccess rule will solve this problem. I tried all those options. Nothing worked for me.
I am using xampp and location of .htaccess file is /htdocs/myapp/.htaccess
.htaccess file is accessible, because if I add any junk data in it, url throw error.
Please help me on this. I need server to return url as it is so that angularjs can route pages correctly. Please help me to write .htaccess rule. mod_rewrite is already enabled in httpd.conf file of apache.
I can able to find solution for this. My .htaccess file will look like below
Options +FollowSymLinks
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index.html
RewriteCond %{REQUEST_URI} !.*\.(map|cssĀ¦js|html|png)
RewriteRule (.*) index.html [L]
</ifModule>

resolving index.php issue at url laravel htaccess

I know this question is asked alot and there are alot of answers on "how to resolve index.php issue in laravel".
However, I tried multiple ways in order to solve it from .htaccess on live dedicated server with WebsitePanel in order to remove index.php from URL. I tried a couple of ways however let me describe the issue here, index.php not only have changed the url, but also some of the css and javascript stuff are not loaded as well while most of them are loaded, CSS seems pretty fine but fonts are also changed.
It was working well at a shared host but here in a dedicated host it has alot of problems.
Is there a .htaccess code to solve this issue? and does index.php affects on CSS and is it normal?
my current htaccess is as follow:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>
You don't need to modify index.php or .htaccess. If you did this, revert to original files.
To remove index.php or public from URLs, you need to setup virtual host for Laravel project (there are instructions both for nginx and apache).
After that restart web server.
This is the most common problem in Laravel. When you upload your code to a webserver, your page will be in
www.web-something.com/index.php
So there are 2 things you should do,
1.Change the .htaccess file and also
2.Change the Virtual Host Settings in your server. Link :for setting up a virtual host in apache
After this you will be able to visit the directory just fine. Also about the CSS , problem , Its probably because of the virtual host settings. Itll get fixed up once you point everything to the right directory.
For IIS Windows Server, the .htaccess needs to be translated to web.config and this works like a charm.
here is the link:
How to enable modrewrite on IIS Web Server
In case Someone needed this in future!

Nginx - Rewrite timestamped assets

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

.htaccess URL Rewrite Failing - The requested URL was not found on this server

I have a very simple rewrite rule. My entire .htaccess file is as follows:
RewriteEngine On
ReWriteRule ^([a-zA-Z0-9\-]+)$ /index.php?page=$1 [L,NC]
This works perfectly on one of my development machines running Apache. It does not work however on my other development machine running Apache (mod_rewrite listed in PHP info under apache2handler). Nor does it work on the live server, which I think is running Windows.
I have tried adding the following:
RewriteBase /
I'm no mod_rewrite wizard and I'm sure it's a very simple solution but it is eluding me currently and keep receiving a 404 not found error.
One thing that is different on the two environments in which this doesn't work, is that the site is not in the root of the URL. So for example it is http://localhost/site and www.example.com/site
I have tried various syntax adjustments in the .htaccess file, and also adding the site to the base:
RewriteBase /site
Remove the 'RewriteBase /site' and try removing the / before index.php
The slash should't be there with use of .htaccess
RewriteEngine On
ReWriteRule ^([a-zA-Z0-9\-]+)$ index.php?page=$1 [L,NC]

CakePHP and .htaccess in shared hosting environment

Greetings!
I have CakePHP based app on shared hosting I wonder if there's a way to clean up the url through .htaccess. What bugs me is that I have to have index.php in it or I get a 404:
project.com/index.php/controller/method
Initially I was getting a 404 error no matter what and my host admin ended up setting RewriteEngine off and this is what it looks like now
<IfModule mod_rewrite.c>
RewriteEngine off
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Is there fix for this without the .htaccess? As it is right now, does it pose any type of security risk?
Thanks
You need three .htaccess files :
/.htaccess
/app/.htaccess
/app/webroot/.htaccess
If the one you pasted in your question is the one at the root of your website, that's probably where your problem comes from. These directives file would rewrite URLs to project.com/webroot/, which doesn't exist. It should redirect to project.com/app/webroot/, which will in turn rewrite to index.php?url=$1 (relative to project.com/app/webroot/).
I'm not pasting the files here; the three of them are available in the CakePHP releases as well as in the Book: http://book.cakephp.org/2.0/en/installation/url-rewriting.html (check the 3rd item in the page).
Are you sure the mod_rewrite module is enabled on your shared hosting?

Resources