59

I am currently migrating my website from Apache to nginx, but my .htaccess file is not working. My website is inside the /usr/share/nginx/html/mywebsite folder. How can I use .htaccess in my nginx server?

This is my .htaccess file:

RewriteEngine on
RewriteRule video/watch/([a-zA-Z0-9_@$*-]+)/?$ "videos-single.php?id=$1" [NC]

4 Answers 4

66

Nginx doesn't support .htaccess (see here: "You can’t do this. You shouldn’t. If you need .htaccess, you’re probably doing it wrong.").

You've two choices (as I know):

4
  • I think /etc/nginx/nginx.conf - see the nginx package's filelist.
    – uzsolt
    Commented Mar 5, 2016 at 10:02
  • 5
    What if you are using a shared server? You don't have access to /etc/nginx then is there a config file similar to .htaccess for the folder where your subdomain is? Commented Jan 7, 2018 at 16:12
  • 2
    @NaturalBornCamper Then I guess you will have to use Apache.
    – Uri
    Commented Jul 16, 2019 at 9:53
  • 1
    @NaturalBornCamper I think you will have your own config file for your site, you either contact your hosting provider or if you manage it you specify you configuration for your site's domain only
    – moghwan
    Commented Jan 5, 2023 at 11:17
27

Disclosure: I am the author of htaccess for nginx, which is now open source software.

Over the past years, I created a plugin which implements htaccess behaviour into nginx, especially things like RewriteRule, Allow and Deny, which can be crucial for web security. The plugin is used in my own productive environments without a problem.

I totally share the point of efficiency and speed in nginx, and why they didn't implement htaccess. However, think about it. You cannot make it worse if you're using nginx plus htaccess. You still keep the great performance of nginx, plus you can drive your legacy appliances effortlessly on one webserver.

12
  • 1
    Too hard to integrate your software. You should write a full tutorial
    – Tony
    Commented Nov 4, 2020 at 16:19
  • Can you check this error: "failed to load external Lua file "/etc/nginx/htaccess-for-nginx/htaccess-1.2.1-bytecode.lua": /etc/nginx/htaccess-for-nginx/htaccess-1.2.1-bytecode.lua: cannot load incompatible bytecode". It caused Nginx to response 500 error
    – Tony
    Commented Nov 4, 2020 at 16:39
  • 5
    I decided to release my code as an open source project: github.com/e404/htaccess-for-nginx Pull requests are very welcome.
    – Gerald
    Commented Jan 25, 2021 at 20:04
  • 3
    Off topic, but it's a pet peeve. It's a "disclosure" not a "disclaimer". You are disclosing that you are the author.
    – liamvictor
    Commented Sep 30, 2021 at 11:11
  • 2
    @liamvictor See these questions on EL&U and ELL.
    – iBug
    Commented Jun 28, 2022 at 12:08
11

This is not supported officially in nginx. If you need this kind of functionality you will need to use Apache or some other http server which supports it.

That said, the official nginx reasoning is flawed because it conflates what users want to do with the way it is done. For example, nginx could easily check the directories only every 10 seconds / minute or so, or it could use inotify and similar mechanisms. This would avoid the need to check it on every request... But knowing that doesn't help you. :)

You could get around this limitation by writing a script that would wait for nginx config files to appear and then copy them to /etc/nginx/conf.d/. However there might be some security implications - as there is no native support for .htaccess in nginx, there is also no support for limiting allowed configuration directives in config files. YMMV.

1
  • 2
    I don't disagree with the reasoning that scanning for files is useless usage of resources, but it definitely doesn't need to be that extreme. It could be made to look only at the root directory for an extra conf file defined by the end-user. It would need to become its own new conf block (DirectoryOverride?) and reload nginx confs in memory by looking at file modification time. 1) It wouldn't even need to execute at every request; 2) It would allow pre-validating and caching of the end-user conf; 3) It would still allow end-user to use location blocks to protect directories and add rewrites.
    – davidwebca
    Commented Nov 29, 2022 at 22:39
-4

"Is there no nginx way to do bulk redirects using regular expressions that doesn't slow down response times."


Just edit your database with myphpmyadmin.

  1. Open myphpmyadmin select your database then find your "yourprefix_Posts" table.
  2. Open it then click the "Search" tab, then "Find and Replace".
  3. Select "post_content" in the dropdown
  4. In the "Find" field, type URL you want to change: "website.com/oldURL".
  5. In the "Replace" field, type the new URL: "website.com/newURL". (To use regular expression, tick the "Regular Expression" box.)

NOTE: You can test this out by simply leaving the "Replace" field blank.

ALWAYS BACKUP database before making changes. This might sound scary but its really not. Its super simple and can be used to quickly replace just about anbything.

1
  • 2
    This is not a Redirect but a change of the URL.
    – Juburin
    Commented Jan 20, 2023 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.