user2668848
user2668848

Reputation: 45

Nginx: Remove part of the url

I'm looking for the right rewrite rule for my Nginx server. I would like to remove a specific part of the URL.

An Example:

example.com/assets/files -- the real location

to

example.com/files

and

example.com/assets/files/image.jpg

to

example.com/files/image.jpg

and

example.com/assets/themes/css/style.css

to

example.com/themes/css/style.css

I have googled it, but none of the salutations I found worked for me. I just got some 500 errors.

So what rewrite rule do I need and where should I put it in my conf file. Thank you!

Upvotes: 1

Views: 5756

Answers (1)

skbly7
skbly7

Reputation: 1162

According to what you have posted in question. I can figure out that you are try to rename your URL, without changing the exact location of the file.

For achieving this, add these in your server directive:

#Example : 
#Rewriting example.com/themes -> example.com/assets/themes
location /themes {
    rewrite ^/.* http://$server_name/assets/themes permanent;
}
location /files {
    rewrite ^/.* http://$server_name/assets/files permanent;
}

Now your user would be opening for eg: http://example.com/files/image.jpg, and your file on server would be placed at location /var/www/..something.../example.com/assets/files/image.jpg

Hope this helps..! :)

Upvotes: 1

Related Questions