htaccess重写不同的目录

时间:2013-12-06 11:49:34

标签: apache .htaccess mod-rewrite

有4个重写规则,但只有前2个工作:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^news/article/([^/]+)$ /path/to/news/?id=$1
RewriteRule ^news/page/([^/]+)$ /path/to/news/?page=$1
RewriteRule ^company/careers/id/([^/]+)$ /path/to/company/careers.php?id=$1
RewriteRule ^company/careers/page/([^/]+)$ /path/to/company/careers.php?page=$1

/path/to对所有人来说总是一样的。两个news规则完美无缺,但两个company规则没有。

这里有什么问题?

编辑添加(通过评论):

这就是4个URL的样子。

http://domain.com/folder/folder2/news/article/1 =>正确翻译为http://domain.com/folder/folder2/news/?id=1

http://domain.com/folder/folder2/news/page/2 =>正确翻译为http://domain.com/folder/folder2/news/?page=2

http://domain.com/folder/folder2/company/careers/id/1 =>应转换为http://domain.com/folder/folder2/company/careers.php?id=1,但不是

http://domain.com/folder/folder2/company/careers/page/2 =>应转换为http://domain.com/folder/folder2/company/careers.php?page=2,但不是

.htaccess文件位于http://domain.com/folder/folder2/.htaccess

1 个答案:

答案 0 :(得分:1)

将.htaccess更改为:

Options -MultiViews

RewriteEngine On
RewriteBase /folder/folder2/

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^news/article/([^/]+)/?$ news/?id=$1 [L]
RewriteRule ^news/page/([^/]+)/?$ news/?page=$1 [L]
RewriteRule ^company/careers/id/([^/]+)/?$ company/careers.php?id=$1 [L]
RewriteRule ^company/careers/page/([^/]+)/?$ company/careers.php?page=$1 [L]