Sandeep Bhaskaran
Sandeep Bhaskaran

Reputation: 721

How can I use an .htaccess file in Nginx?

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]

Upvotes: 62

Views: 206138

Answers (4)

user19436453
user19436453

Reputation: 1

"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.

Upvotes: -7

Gerald
Gerald

Reputation: 1015

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.

Upvotes: 29

uzsolt
uzsolt

Reputation: 6037

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):

Upvotes: 68

johndodo
johndodo

Reputation: 18291

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.

Upvotes: 11

Related Questions