NGINX: remove part of url permanantly
server
{
listen 80; ## Listen on port 80 ##
server_name example.com; ## Domain Name ##
index index.html index.php; ## Set the index for site to use ##
charset utf-8; ## Set the charset ##
location ^~ /forum/showPost {
rewrite ^/forum/showPost(.*)$ $1 permanent;
}
location ^~ /business/showDetails {
rewrite ^(.*)business/showDetails(.*)$ classifieds$1 permanent;
}
}
There are a number of options. You could protect the rewrite within a location block which would be quite efficient as the regular expression is only tested if the URI prefix matches:
location ^~ /forum/showPost {
rewrite ^/forum/showPost(.*)$ $1 permanent;
}
See this document for more.
You used permanent in your question - which generates a 301 response.
If you use redirect
instead of permanent
- a 302 response will be generated.
If you use last
instead of permanent
- an internal redirect will occur and the browser address bar will continue to show the old URL.
In response to your comment:
rewrite ^/forum/showPost(.*)$ /post$1 permanent;