Apache mod_rewrite别名为另一个路径的路径

时间:2012-10-04 04:03:45

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

由于一些Drupal问题,我有一个奇怪的重写问题。我不知道如何修复Drupal问题,所以这将是一个临时修复:

路径/the%20campanile/([0-9]+)/([0-9]+)/(最好使用斜杠可选)应该是/campanile/([0-9]+)/([0-9]+)/的别名。

我试过了:

RewriteBase /
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/

除了404失败(大概是重写错误)。我也尝试过使用通配符(*),但无济于事。我查看了StackOverflow和其他一些论坛上的大约16篇文章。

整个.htaccess看起来像这样:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]

我做错了什么?

1 个答案:

答案 0 :(得分:1)

当您尝试匹配^campy/([0-9]+)/([0-9]+)/$之类的内容时,不确定为什么正则表达式为/the%20campanile/([0-9]+)/([0-9]+)/。除了分组,坎比永远不会匹配%20campanile

您需要记住,URI在通过mod_rewrite处理之前已经被解码,因此您不希望与%20匹配,而只是与空间匹配:

RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]

您还希望这些规则之前您的index.php路由规则,因为路由规则会在它达到您的其他规则之前重写它:

RewriteEngine on
RewriteBase /
RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
相关问题