【发布时间】:2014-01-09 14:20:07
【问题描述】:
我正在尝试将旧的 URL 结构 mysite.com/blog/my-blog-post 重写为新的 URL 结构 mysite.com/blog/post/my-blog-post。目前,我的 .htaccess 文件中有这条规则:
RewriteRule ^blog/(.*)$ /blog/post/$1 [R=301,L]
这是输出 mysite.com/blog/post/post/post/post/post/post/post/.../my-blog-post。
似乎我的重写一直匹配,因此再次重写。然后再次。然后再次。我尝试添加:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
但这并没有帮助。我想知道是不是因为博客文章来自 CMS/数据库,而不是指向的实际文件。
为了让博客的新结构更加复杂,我将类别放在 mysite.com/blog/my-category-name 上。我也有 mysite.com/blog/author/author-name。我不想也不需要重写这些 URL。这是可行的吗?我是否应该在我的类别 URL 中添加“类别”一词以简化此操作?
这里只是作为参考,我的 .htaccess 中的其他重写规则:
# For more awesome .htaccess rules and optimisations
# checkout the HTML5 Boilerplate .htaccess files
# https://github.com/paulirish/html5-boilerplate/blob/master/.htaccess
# Although highly unlikely, your host may have +FollowSymLinks enabled at the root level,
# yet disallow its addition in .htaccess; in which case,
# adding +FollowSymLinks will break your setup (probably a 500 error),
# so just remove it, and your rules should work fine.
Options +FollowSymlinks
# EE 404 page for missing pages
# ErrorDocument 404 /index.php/{ee:404}
# Simple 404 for missing files
<FilesMatch "(\.jpe?g|gif|png|bmp|css|js|flv)$">
ErrorDocument 404 "File Not Found"
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
</IfModule>
# Blog redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)$ /blog/post/$1 [R=301,L]
# RedirectMatch ^/blog/$ /blog/post/$1 [R=301,L] --> Didn't work
# Block access to "hidden" directories whose names begin with a period. This
# includes directories used by version control systems such as Subversion or Git.
<IfModule mod_rewrite.c>
RewriteRule "(^|/)\." - [F]
</IfModule>
# remove the www - Uncomment to activate
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
# Remove the trailing slash to paths without an extension
# Uncomment to activate
<IfModule mod_rewrite.c>
RewriteRule ^(.*)/$ /$1 [R=301,L]
</IfModule>
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
【问题讨论】: