62 lines
2.1 KiB
Markdown
62 lines
2.1 KiB
Markdown
|
---
|
||
|
title: "Permanently redirect WordPress pages"
|
||
|
kind: article
|
||
|
slug: permanently-redirect-wordpress-pages
|
||
|
created_at: 2008-04-15
|
||
|
tags:
|
||
|
- 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:
|
||
|
|
||
|
<pre lang="text"># BEGIN WordPress
|
||
|
<IfModule mod_rewrite.c>
|
||
|
RewriteEngine On
|
||
|
RewriteBase /
|
||
|
RewriteCond %{REQUEST_FILENAME} !-f
|
||
|
RewriteCond %{REQUEST_FILENAME} !-d
|
||
|
RewriteRule . /index.php [L]
|
||
|
</IfModule>
|
||
|
|
||
|
# END WordPress</pre>
|
||
|
|
||
|
As I said, it's <strong>generated</strong> 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 <a href="http://ariejan.net/pages/svnsheet">pages/svnsheet</a> to <a href="http://ariejan.net/svncheatsheet">http://ariejan.net/svncheatsheet</a>.
|
||
|
|
||
|
<pre lang="text"><IfModule mod_rewrite.c>
|
||
|
RewriteEngine On
|
||
|
RewriteBase /
|
||
|
RewriteCond %{REQUEST_FILENAME} !-f
|
||
|
RewriteCond %{REQUEST_FILENAME} !-d
|
||
|
RewriteRule ^pages/svnsheet$ http://ariejan.net/svncheatsheet [R=301,L]
|
||
|
</IfModule>
|
||
|
|
||
|
# BEGIN WordPress
|
||
|
<IfModule mod_rewrite.c>
|
||
|
RewriteEngine On
|
||
|
RewriteBase /
|
||
|
RewriteCond %{REQUEST_FILENAME} !-f
|
||
|
RewriteCond %{REQUEST_FILENAME} !-d
|
||
|
RewriteRule . /index.php [L]
|
||
|
</IfModule>
|
||
|
|
||
|
# END WordPress</pre>
|
||
|
|
||
|
FYI, the R=301 means the browser (or search bot) receives a "Moved Permanently" message. L means this is the <strong>l</strong>ast 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.
|