0

I'm trying to implement simple url rewriting:

this link: http://www.mysite.it/DN1

should load http://www.mysite.it/chi-siamo

I have inplemented this code in function.php file of the theme:

function custom_rewrite_basic1() {
    echo("ciao4");
  add_rewrite_rule('^DN1/', '/chi-siamo/', 'top');
}
add_action('init', 'custom_rewrite_basic1');

but when I load the link, I get the 404 page.

1

Theme's functions.php is not the best place to do these sort of URL rewrites.

Why?: because in that case WordPress will be fully loaded and then redirected to a new link and then fully loaded again to show the page. That's a lot of delay for a page view.

Instead of that, you may use URL rewrite in .htaccess (assuming your web server is Apache):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# YOUR CUSTOM REDIRECT
RewriteRule ^DN1$ http://www.mysite.it/chi-siamo [L,R=301,NC]

# default WordPress rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.