用两个变量重写规则问题

时间:2016-01-08 13:17:56

标签: apache .htaccess mod-rewrite url-rewriting

我正在尝试引入两个URL路径重写但由于某种原因它无法按预期工作。

我正在尝试重写: 在第一个实例中, /styles/democlient/main.css _projects / democlient / public / styles / main.css 。第二个“样式”规则按预期工作,只有第一个没有。

我将LogLevel alert rewrite:trace6添加到我的Apache vhosts配置中,以便更容易跟踪。在我看来,当mod达到第一个“风格”规则时,它不会停止并且由于某种原因试图将它与第二个匹配。

我的.htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule styles/([a-z0-9]+)/(.*) _projects/$1/public/styles/$2 [NC,L]
RewriteRule styles/(.*) public/styles/$1 [NC,L]
RewriteRule (.*)/(.*) index.php?page=$1&subpage=$2 [NC,L,QSA]
RewriteRule (.*) index.php?page=$1 [NC,L,QSA]

我的日志

[perdir E:/www/WEB_tech4rec/] add path info postfix: E:/www/WEB_tech4rec/styles -> E:/www/WEB_tech4rec/styles/democlient/main.css
[perdir E:/www/WEB_tech4rec/] strip per-dir prefix: E:/www/WEB_tech4rec/styles/democlient/main.css -> styles/democlient/main.css
[perdir E:/www/WEB_tech4rec/] applying pattern 'styles/([a-z0-9]+)/(.*)' to uri 'styles/democlient/main.css'
[perdir E:/www/WEB_tech4rec/] rewrite 'styles/democlient/main.css' -> '_projects/democlient/public/styles/main.css'
[perdir E:/www/WEB_tech4rec/] add per-dir prefix: _projects/democlient/public/styles/main.css -> E:/www/WEB_tech4rec/_projects/democlient/public/styles/main.css
[perdir E:/www/WEB_tech4rec/] strip document_root prefix: E:/www/WEB_tech4rec/_projects/democlient/public/styles/main.css -> /_projects/democlient/public/styles/main.css
[perdir E:/www/WEB_tech4rec/] internal redirect with /_projects/democlient/public/styles/main.css [INTERNAL REDIRECT]

[perdir E:/www/WEB_tech4rec/] strip per-dir prefix: E:/www/WEB_tech4rec/_projects/democlient/public/styles/main.css -> _projects/democlient/public/styles/main.css
[perdir E:/www/WEB_tech4rec/] applying pattern 'styles/([a-z0-9]+)/(.*)' to uri '_projects/democlient/public/styles/main.css'
[perdir E:/www/WEB_tech4rec/] strip per-dir prefix: E:/www/WEB_tech4rec/_projects/democlient/public/styles/main.css -> _projects/democlient/public/styles/main.css
[perdir E:/www/WEB_tech4rec/] applying pattern 'styles/(.*)' to uri '_projects/democlient/public/styles/main.css'
[perdir E:/www/WEB_tech4rec/] rewrite '_projects/democlient/public/styles/main.css' -> 'public/styles/main.css'
[perdir E:/www/WEB_tech4rec/] add per-dir prefix: public/styles/main.css -> E:/www/WEB_tech4rec/public/styles/main.css
[perdir E:/www/WEB_tech4rec/] strip document_root prefix: E:/www/WEB_tech4rec/public/styles/main.css -> /public/styles/main.css
[perdir E:/www/WEB_tech4rec/] internal redirect with /public/styles/main.css [INTERNAL REDIRECT]
[perdir E:/www/WEB_tech4rec/] strip per-dir prefix: E:/www/WEB_tech4rec/public/styles/main.css -> public/styles/main.css
[perdir E:/www/WEB_tech4rec/] applying pattern 'styles/([a-z0-9]+)/(.*)' to uri 'public/styles/main.css'
[perdir E:/www/WEB_tech4rec/] strip per-dir prefix: E:/www/WEB_tech4rec/public/styles/main.css -> public/styles/main.css[perdir E:/www/WEB_tech4rec/] applying pattern 'styles/(.*)' to uri 'public/styles/main.css'
[perdir E:/www/WEB_tech4rec/] rewrite 'public/styles/main.css' -> 'public/styles/main.css'
[perdir E:/www/WEB_tech4rec/] add per-dir prefix: public/styles/main.css -> E:/www/WEB_tech4rec/public/styles/main.css
[perdir E:/www/WEB_tech4rec/] initial URL equal rewritten URL: E:/www/WEB_tech4rec/public/styles/main.css [IGNORING REWRITE]

如果你能帮我找到原因,那就太好了。

1 个答案:

答案 0 :(得分:0)

您需要在样式RewriteRules之前添加^,并阻止您的上一个RewriteRules重定向物理文件。

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^styles/([a-z0-9]+)/(.*) _projects/$1/public/styles/$2 [NC,L]
RewriteRule ^styles/(.*) public/styles/$1 [NC,L]

RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$
RewriteRule (.*)/(.*) index.php?page=$1&subpage=$2 [NC,L,QSA]
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$
RewriteRule (.*) index.php?page=$1 [NC,L,QSA]