regex - 301 将所有博客文章重定向到新的子文件夹

标签 regex apache .htaccess mod-rewrite seo

我目前正在使用这个 .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/

相关文章:

javascript - array.length 永远不会等于零并停止程序

regex - 使用查询参数限制对URL的访问

php - .htaccess 重定向错误和循环

php - 加号登录GET查询并比较URL

java - 在 Java 中读取文件、替换单词并写入新文件

MySQL:拆分字段,对每个部分进行字符串替换和连接,然后重新加入

Python httpd 将监听已使用的端口,而不会在 Windows 中抛出套接字错误,但在 OS X 中不会?

php - 在 Python WSGI 环境中运行 PHP 脚本

.htaccess - 强制浏览器忘记缓存的重定向?

regex - 为什么在这个 Perl 正则表达式中 `\s+` 比 `\s\s*` 快得多?