ZgrKARALAR
ZgrKARALAR

Reputation: 489

Nginx - Wordpress redirect to default url

I have one problem about the redirecting I want to make redirect to example.com/12321.html to example.com?p=12321.

I was user before url typle like first number things after this i change my system to seo url with content name to url. Now i guest best system to redirect to ?p=

What I Try

First i try to change that on .htaccess not working i use this code

RewriteEngine On
RewriteRule "^([0-9]+)+[^.html]" "https://www.example.com?p=$1" 

Than I thing to make that with nginx write this code on nginx server block

rewrite ^([0-9]+)+[^.html] https://www.example.com/$1 redirect;

I don't understant that is why not working, I check it already on https://regex101.com/r/pimHGN/1 showing working there.

Upvotes: 1

Views: 1058

Answers (2)

AfroThundr
AfroThundr

Reputation: 1225

Ok, as a quick check, you could try a regex like the following for Apache:

RewriteRule ^/([0-9]+)\.html$ https://www.example.com?p=$1 [L,R=301]

And something like this for Nginx:

rewrite ^/([0-9]+)\.html$ https://www.example.com?p=$1 permanent;

This should match any page composed of digits 0-9 and .html.

The rule you posted wouldn't match on the .html part, which might be why it wasn't working.

Upvotes: 2

cnst
cnst

Reputation: 27218

Not too clear what you're trying to do, however, unless you have a rewrite rule that'd be removing / from $uri first, your rule of rewrite ^([0-9]+)+[^.html] https://www.example.com/$1 redirect; would never match, because it is impossible for $uri to not start with /, hence, ^([0-9]+) would never catch anything. (Additionally, [^.html] looks suspect as well — I have a feeling that it wouldn't do what you expect it might.)

The correct way may be:

rewrite ^/([0-9]+)\.html$ /?p=$1 redirect;

However, as per nginx redirect loop, remove index.php from url and related, the following "loop" might be what you actually want:

if ($request_uri ~ ^/?p=([0-9]+)) {
    return 302 /$1.html
}
rewrite ^/([0-9]+)\.html$ /?p=$1;

The above code would ensure that the /?p= style is only used internally within nginx and the backend, whereas externally, everything always gets redirected to, and served from, the .html-style pages.

Upvotes: 1

Related Questions