我目前正在使用这个 .htaccess 重写来为我的博客文章获取漂亮的 url:
RewriteRule ^([a-zA-Z0-9-]+)$ /item.php?slug=$1 [L]
这最终看起来像:
example.com/my-new-awesome-blog-post
我想要的是将我所有的帖子移动到一个名为 items 的新文件夹中,这样它看起来像:
example.com/items/my-new-awesome-blog-post
我不仅要将所有博文移动到子文件夹,还要确保它是正确的 301 重定向,这样 SEO 就没有问题。
感谢您的帮助/建议。
最佳答案
您可以在您的 DOCUMENT_ROOT/.htaccess
文件中使用此代码:
Options -MultiViews
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+items/ [NC]
RewriteRule ^ items%{REQUEST_URI} [L,NE,R=302]
RewriteRule ^items/([a-zA-Z0-9-]+)/?$ items/item.php?slug=$1 [L,QSA]
关于regex - 301 将所有博客文章重定向到新的子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30243370/