301将localhost / * / path重定向到localhost / * / path /

时间:2013-09-10 15:30:01

标签: regex apache .htaccess mod-rewrite http-status-code-301

以前曾提出过类似的问题,但目前尚不清楚!

我有一个像这样的文件夹结构:

http://example.com/category1/subcat1/path1
http://example.com/category2/subcat2/path2
http://example.com/category3/subcat3/path3

我想将它们分别重定向到以下内容:

http://example.com/category1/subcat1/stuff1
http://example.com/category2/subcat2/stuff2
http://example.com/category3/subcat3/stuff3

注意:如果exam​​ple.com/path不存在,我需要example.com/path的任何请求重定向到example.com/path/!

我试过了:

RewriteEngine On
RewriteBase /var/www/
RewriteRule ^path1/$ path1 [R=301,L]
RewriteRule ^path1/(.*)$ path1 [R=301,L]

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

你需要摆脱重写基础。它是文件路径而不是网址路径,因此您最终会被重定向到http://example.com/var/www/path1

对于尾部斜杠,您需要:

RewriteEngine On

RewriteRule ^path1$ /path1/ [R=301,L]

其余的:

RewriteRule ^category1/subcat1/path1$ /category1/subcat1/stuff1 [R=301,L]
RewriteRule ^category2/subcat2/path2$ /category2/subcat2/stuff2 [R=301,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [L,R=301]