Reputation: 8819
Can anybody please help me to remove first directory name from URL?
My Image location is _data/hotel/3/15377/hotel_image.jpg
But Image path gets changed due to relative URL in code and it become to something like this.
example.com/france/_data/hotel/3/15377/hotel_image.jpg
example.com/usa/_data/hotel/3/15377/hotel_image.jpg
example.com/india/_data/hotel/3/15377/hotel_image.jpg
is their any possibilities to remove dynamic country name from above URL
Upvotes: 1
Views: 4490
Reputation: 8895
@Ivan Shatsky's answer is great for files but also if we want to redirect a general url is better if you use the rewrite
directive.
Depending where you define the rewrite
directive you have two ways to implement it:
server
contextserver {
...
rewrite ^/[a-z]+/_data/(.+)$ /_data/$1 last;
...
}
location
contextlocation ~ /[a-z]+/_data/(.+) {
rewrite ^/[a-z]+/_data/(.+)$ /_data/$1 break;
proxy_pass http://backend;
}
Teo, why did you change the flag to break
?* Because, if this directive is put inside of a location
context, the last
flag might make nginx
to run 10 cycles and return the 500 error.
Remember not add /
at the end of the proxy_pass
directive. This example wont work:
...
proxy_pass http://backend/;
...
Upvotes: 2
Reputation: 15537
If you want to rewrite only this particular URL, you can use this location
block in your config:
location ~ /[a-z]+/_data/hotel/3/15377/hotel_image.jpg {
try_files /_data/hotel/3/15377/hotel_image.jpg;
}
If you want to rewrite all URLs which lead to /<country>/_data/...
, you can use:
location ~ /[a-z]+/_data/(.+) {
try_files /_data/$1;
}
or for stricter URL checking:
location ~ /(?:france|usa|india)/_data/(.+) {
try_files /_data/$1;
}
Upvotes: 4