permalinks - redirect old pages by .htaccess

admin2025-06-03  3

I want to redirect pages from sitename/sitename/pagename.html and sitemap/sitename/ to

sitemap/sitename/pagename 

how I can do that by using .htaccess file

I tried this code

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule . /sitename/index.php [L]
RedirectMatch 301 (.*)\.html$ sitename/$1
</IfModule>

and I tried also to add

 Redirect 301 sitename/sitename/

but it is not working but I think both not working because it still gives me an error when I open www.sitename/pagename.html

I want to redirect pages from sitename/sitename/pagename.html and sitemap/sitename/ to

sitemap/sitename/pagename 

how I can do that by using .htaccess file

I tried this code

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule . /sitename/index.php [L]
RedirectMatch 301 (.*)\.html$ sitename/$1
</IfModule>

and I tried also to add

 Redirect 301 sitename/sitename/

but it is not working but I think both not working because it still gives me an error when I open www.sitename/pagename.html

Share Improve this question edited Feb 18, 2019 at 9:15 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Mar 14, 2013 at 13:43 sarahsarah 672 silver badges11 bronze badges 1
  • In trying to "exemplify" the code you've actually changed its meaning and made it invalid!? – MrWhite Commented Feb 17, 2019 at 22:18
Add a comment  | 

1 Answer 1

Reset to default 0

%postname% is not a mod_rewrite rewrite tag. You are going to have to write out the path. mod_rewrite operates before WordPress, or PHP, gets involved. You don't have access to any of that functionality.

If your "contact" page-- the page named "contact"-- has ID 213 you shouldn't have to do anything to redirect, though. This will happen when you enable pretty permalinks.

You can also redirect using WordPress functionality by doing something like:

function redirect_213() {
  if (is_page(213)) {
    wp_safe_redirect(wp_safe_redirect(home_url('/contact')));
  }
}
add_action('template_redirect','redirect_213');

This will probably perform slightly less efficiently than the .htaccess but you may not notice.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748895860a314621.html

最新回复(0)