So, I have WP with almost classic permalink structure, like: /%category%/%year%/%month%/%day%/%postname%/
I would like to redirect with 301 error code to new style, like /%year%/%month%/%day%/%postname%/ or simple /%postname%/.
I've made this rule, but they return same URL.
location ~ "^\/([a-zA-Z0-9_.-]+)\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([a-zA-Z0-9_.-]+)\/$" {
rewrite ^(.*) $1 permanent;
}
Or, for /%postname%/ I've tried code like this:
location ~ "^\/([a-zA-Z0-9_.-]+)\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([a-zA-Z0-9_.-]+)\/$" {
rewrite ^(.*) $5 permanent;
}
But this code return empty response.
Please, help me solve the issue. I can't catch were I'm wrong. Thanks!
So, it was very simple:
location ~ "^/([a-zA-Z0-9_.-]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/([a-zA-Z0-9_.-]+)/$" {
rewrite ^(.*)/(.*)/(.*)/(.*)/(.*)/ /$5/ permanent;
}
This rule rewrite /category/year/month/day/postname/ to /postname/ URL in WP with 301 error code for search engines.
Related
I have a MantisBT installation that I want to redirect to GitHub. For specific issues, there exists a corresponding issue at GitHub, so I want to treat those 'view' URIs first. Then all other URIs should just go to 'issues'.
# send the bare domain to GitHub issues
rewrite ^/$ https://github.com/Slicer/Slicer/issues/ permanent;
# https://issues.slicer.org/view.php?id=4725 => https://github.com/Slicer/Slicer/issues/4725
location ~ ^/view.php {
# rewrite ^.*$ https://github.com/Slicer/Slicer/issues/$arg_id permanent;
return 301 https://github.com/Slicer/Slicer/issues/$arg_id;
}
# Send all remaining URIs to github issues
rewrite ^ https://github.com/Slicer/Slicer/issues permanent;
The problem I'm having is that the view.php rule is not working properly. I'm getting
https://github.com/Slicer/Slicer/issues?id=1234 instead of
https://github.com/Slicer/Slicer/issues/1234
I simplified my configuration to the following, and it's working now as intended.
# View URIs like https://issues.slicer.org/view.php?id=4725
location ~ ^/view.php {
# rewrite ^.*$ https://github.com/Slicer/Slicer/issues/$arg_id last;
return 301 https://github.com/Slicer/Slicer/issues/$arg_id;
}
# everything else
location ~ (?!view) {
rewrite ^(.*)$ https://github.com/Slicer/Slicer/issues? permanent;
}
Current API end points are like
domain.com/api/rest/[actual_module_routing_path, eg. cms/page/home]
In the new system this has to be something like
domain.com/rest/V1/$1
It has to work regardless of HTTP Method (GET, POST, PUT, DELETE). Trying something like this in nginx host config and I couldn't get it working, some help will be really appreciated
location /api/rest {
#Rewrite $uri=/api/rest/ back to just $uri=/rest/V1/$1
rewrite ^/api/rest/(.*)$ /rest/V1/$1 break;
}
location ~ /api/rest/(.*)
{
rewrite ^(.*)$ /rest/v1/$1 redirect;
}
Very simple rewrite, but it suddenly doesn`t work.
Need one link 301 to other, but "some-text-here" must be parsed and used in var before
www.host.com/some-text-here/url1/
to
www.host.com/some-text-here/url2/
if ( $request_filename ~ ([^.*]+)/url1/ ) {
rewrite ^ $1/url2/? permanent;
}
You don't need if. This line should be enough:
rewrite ^/(.+)/url1/ /$1/url2/? permanent;
I was trying to find sollytion? but fails
I want to redirect clients to another site, but part of url (XXX) must be the same
original www.mysite.com/card.aspx?service_id=XXX&pageuid=card
rewrite to https://some.othersite.com/?s=XXX
XXX are same
Who can help? If it possible?
location ~ /card(.*)$ {
if ($args ~* /?service_id=[0-9]+) {
rewrite ^ https://some.othersite.com?s=$arg_service_id? permanent;
}
}
hope it will be usefull for somebody.
I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}