Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


[Ask] Nginx Rewite URL
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

[Ask] Nginx Rewite URL

BayuBayu Member

Can someone help me with nginx rewrite rules?
I want redirect url permanently from http://domain.com/XYZ/posttitlename.html to http://domain.com/posttitlename.html

XYZ is number generated by wordpress (postid) from 1 to 9999.

Currently I am using a plugin to redirect url, but it seems better when directly handled by nginx. Thank you

Comments

  • vfusevfuse Member, Host Rep

    rewrite ^/([0-9{1,4}])/(.*)$ http://domain.com/$2 redirect;

  • I'd replace [0-9]{1,4} with \d+ so that any number matches, not just 1-9999.

  • rokokrokok Member

    Yup possible your postid hits more than 4digit numbers better use ([0-9]+)

  • BayuBayu Member
    edited April 2015

    Seems rewrite rules not working properly.

    Nginx log: directive "rewrite" is not terminated by ";"... in /nginx/conf/virtual.conf:16

    I also have tried to put inside location directive of server block

        location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
                rewrite ^/([0-9{1,4}])/(.*)$ http://domain.com/$2 redirect;
        }
    

    @rokok said:
    Yup possible your postid hits more than 4digit numbers better use ([0-9]+)

    Yes, is possible my postid hit more than 4 digit numbers, but I don't add new post again. It was my old blog.

    Can you help me to fix my nginx virtualhost conf?

  • rokokrokok Member

    pastebin, maybe others can help

    rewrite ^/([0-9]+)/(.*)(/?)+$ /$2 redirect;

    i know nothing about nginx jon snow, ngasal

  • I'm going to recommend two things. First, change the rewrite rule to

    rewrite ^/[0-9]+/(.*) http://domain.com/$1 permanent;
    

    This better captures the URLs and also does a 301 redirect so search engines, etc. know that the change is permanent (since you said you're not going back).

    Second, check line 16 since that's the error in your Nginx conf file. If necessary, strip identifiable information (such as the domain name) from your file and post it.

Sign In or Register to comment.