Permanently redirect WordPress pages
source link: https://www.devroom.io/2008/04/15/permanently-redirect-wordpress-pages/?utm_campaign=Feed%3A+ariejan+%28ariejan%7Cdevroom.io%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Permanently redirect WordPress pages
Posted:
2008-04-15
- Last updated:
2019-06-05
Tagged general wordpress apache apache2 rewrite mod_rewrite permalinks
After my trip to Mephisto some time back, I noticed that some pages were accessible from different URLs. After moving back to WordPress, these permalinks no longer work.
I’m running WordPress with Apache2, so it shouldn’t be too hard redirect those old permalinks to their new locations. (That’s what rewriting is all about anyway).
Here is the default .htaccess file generated by WordPress:
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
As I said, it’s generated by WordPress. It would be very unwise to edit this, because our changes might get lost in the future.
The solution is simple. Add another block above the generated one that does permanent redirects. In this example I rewrite pages/svnsheet to http://ariejan.net/svncheatsheet.
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^pages/svnsheet$ http://ariejan.net/svncheatsheet [R=301,L] # BEGIN WordPress RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
FYI, the R=301 means the browser (or search bot) receives a “Moved Permanently” message. L means this is the last rewrite rule to read.
Of course, you can go nuts and add all sorts of funky regular expressions to rewrite URLs.
The nice thing is that WordPress will only replaces the block between the BEGIN and END tags, keeping your rewrites in tact.
Note: don’t forget to restart apache if your rewrites don’t seem to work.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK