rewriterrule匹配正则表达式,但不转发


RewriteRule matches regular expression but isn't forwarding

我"只是"尝试转换url,如

[AWEBSITE]/使用bgh/文章/10/08/2009/left-4-dead-2-scavenge-mode-announced

[AWEBSITE]/使用bgh left-4-dead-2-scavenge-mode-announced

正则表达式匹配正确(使用http://www.regular-expressions.info/javascriptexample.html进行测试),但是什么也没有发生。我尝试过类似的规则,使用RedirectMatch 301代替:

RedirectMatch 301 ^/bgh/articles/([0-9]{2})/([0-9]{2})/([0-9]{4})/([^/]+)$ /bgh/$4?

URL转换正确,但在末尾留下一个"?"。如果我从该行删除问号,它会正确转换URL,但会在末尾重新附加一个查询字符串。下面是我在。htaccess中使用的内容:

AddType application/x-httpd-php53 .php
RewriteEngine on
RewriteRule ^bgh/articles/([0-9]{2})/([0-9]{2})/([0-9]{4})/(.*)$ bgh/$4
RewriteCond %{HTTP_HOST} ^awebsite'.com$ [OR]
RewriteCond %{HTTP_HOST} ^www'.awebsite'.com$
RewriteRule ^/?$ "http':'/'/www'.awebsite'.com'/bgh"
AddHandler application/x-httpd-php5 .html

此外,我正在使用Drupal和似乎是旧版本的Apache(例如,QSD标志破坏了我的。htaccess)。我在扯我的头发,我希望有一些非常简单的东西,我在这里错过了。

试试这个:

AddType application/x-httpd-php53 .php
AddHandler application/x-httpd-php5 .html
RewriteEngine on
RewriteRule ^bgh/articles/([0-9]{2})/([0-9]{2})/([0-9]{4})/(.*)$ bgh/$4? [L,NC,R=302]
RewriteCond %{HTTP_HOST} ^(www'.)?awebsite'.com$ [NC]
RewriteRule ^/?$ http://www.awebsite.com/bgh [L,R]

假设您正在使用Apache httpd 2.4,您可以将[QSD]标记添加到rewriterrule的末尾,以删除(丢弃)查询字符串。如果您使用的是2.2或更早版本,则[QSD]标志不可用。然而,那是尾随吗?实际上并没有什么害处,而且大多数网站用户根本不会看URL。如果你这样做是为了想象中的"SEO"好处,那么你几乎肯定是在浪费时间,没有真正的好处。

还要注意,您不需要(也不应该)转义RewriteRule目标中的字符。因此:

RewriteCond %{HTTP_HOST} ^(www'.)?awebsite'.com$
RewriteRule ^/?$ "http://www.awebsite.com/bgh"