NGINX rewrite url so it removes some part from it - How????

Hi,

I am using NGINX as my webserver and i have a question which i really dont know if it is technically doable. So i start to explain.

1.) I have categorized my web pages under my root so i have lets say 20 different folders and each folder has its .html pages in it. I have to mention that this is a STATIC website. So for example we will have pages like:

www.mydomain.com/folder-A/bla-bla-bla-bla.html
www.mydomain.com/folder-B/talk-talk-talk.html
www.mydomain.com/folder-C/bip-bip-bip.html

2.) Ok… so now comes the part that i am interested in. Can i give some rewrite rule to NGINX so that NGINX removes the folder part from each url? Like a 301 redirect so the urls look like:

www.mydomain.com/bla-bla-bla-bla.html
www.mydomain.com/talk-talk-talk.html
www.mydomain.com/bip-bip-bip.html

I know i can do this by simply throwing ALL .html files into the ROOT directory but the website is very large and has about 3000 .html web documents. So it would be better if i can categorize these 3000 .html files into folders BUT it would be nice that the actual URL shows up different without the folder part.

Is this doable? Without havening duplicate content and being penalized from Google. If this is doable… can someone guide me, give me an eample or help me out somehow to do it right? With a correct 301 permanent redirect etc?

Thanks in advance

George

5 Replies

The only way I can think of is to incorporate the folder name into the URL using a delimiter. This wont shorten your URL at all, but there must be some piece of information in the ultimate URL to let NGinx now where the file is. You can remove the folder slashes and replace with a delimiter, for example "–"… This example is for a simple one folder deep layout as you specified.

rewrite ^/(.+)--(.+)$ /$1/$2 permanent;

Ultimately I don't think you can gain much in terms of shortening, but if you just really want to ditch the folder slashes that can be done easy enough (as above)

Do you not want to move the files simple because of the time it would take to move each file from the subdirectory to the parent? If so, a simple Bash script would do it for you.

@carmp3fan:

Do you not want to move the files simple because of the time it would take to move each file from the subdirectory to the parent? If so, a simple Bash script would do it for you.

I dont want to move the files because i would end up having in the root directory 3000 different .html files… I want to have them organized in directories but i want a different url for the files.

Thanks

George

I can't think of a GOOD solution.

1. Could do 3000 symlinks from root to subdirectories. Script to keep them up to date.

2. Could have nginx scan all 20 directories. Then enable caching with a long expire if your content doesn't change often.

3. Could list all 3000 addresses in a file and use nginx map module to hash em and use them in a single redirect.

Without referring to some list or actually testing the locations, how else can nginx know where to find the file?

I don't know about nginx, but in lighttpd, I'd probably reach for a lua script. See this blarg post for a tangentially-related thing.

… but, if there's a way to "embed" the directory name in the URL somehow, I like the regex idea mentioned previously. Simple and good.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct