Mod Rewrite无法使其正常工作

时间:2014-08-31 00:39:58

标签: php apache .htaccess mod-rewrite

我的现有系统中有一个Mod Rewrite规则

#default Lightweight rewriting
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php [L]

现在我们刚刚购买了一个新的域名allheavy.net,我想将它用作网址缩短器 和目前的系统一样,我有这个乐队页面

http://allheavymetal.net/band/band/id/222/name/Metallica

现在我想要它,所以我可以去 http://allheavy.net/Metallica

更新2 所以这就是我到目前为止所做的事情

# Match the host, optionally with www.
RewriteCond %{HTTP_HOST} ^(www\.)?allheavy\.net$
# Ensure the rule doesn't apply to itself
RewriteCond %{REQUEST_URI} !/index/short
RewriteRule ^(.*)$ /index/short/d/$1 [L]

#Lightweight MVC
RewriteCond %{HTTP_HOST} ^(www.)?allheavymetal\.net$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

因此,第一条规则起作用,第二条规则独立工作

但是,如果第一条规则被称为重写的网址(/index/short/d/Metallica),但在调用第二条规则时则不会使用...

1 个答案:

答案 0 :(得分:1)

您可以将RewriteCond选项与www.合并,将原来的两个()?合并为一个。然后,您需要一个附加条件,以确保后续RewriteRule与处理脚本URI /index/short/d不匹配。

# Match the host, optionally with www.
RewriteCond %{HTTP_HOST} ^(www\.)?allheavy\.net$
# Ensure the rule doesn't apply to itself
RewriteCond %{REQUEST_URI} !/index/short
# This should not match a leading / in directory context
# Use the [L] flag instead of the [C] chain flag to allow
# further rules to match if this does not
RewriteRule ^(.*)$ /index/short/d/$1 [L]
# Or if the same index.php will process this, omit [L]
# allowing the rule to continue execution and match the catch-all
# RewriteRule ^(.*)$ /index/short/d/$1

# Then place your original generic catch-all rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

所有这些假设allheavy.net/index/short/d处的脚本已经能够通过其URL缩短器成功处理和提供目标脚本。

一些额外的研究

RewriteCond %{HTTP_HOST} ^(www\.)?allheavy\.net$
RewriteCond %{REQUEST_URI} !/index/short
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index/short/d/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

以上内容将首先通过/index/short/d/$1重写,然后重写为index.php,但是,/index/short/d/xxxxx中的PHP会收到控制器URI $_SERVER['REDIRECT_URL']。如果您的控制器能够使用它,这可能适合您。