简体   繁体   English

RewriteRule匹配正则表达式但不转发

[英]RewriteRule matches regular expression but isn't forwarding

I'm "simply" trying to transform the URLS like 我“只是”试图像这样转换网址

[AWEBSITE]/bgh/articles/10/08/2009/left-4-dead-2-scavenge-mode-announced [AWEBSITE] / bgh / articles / 10/08/2009 / left-4-dead-2-scavenge-mode-announced

to

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

The regular expression matches properly (as tested using http://www.regular-expressions.info/javascriptexample.html ), but nothing is happening. 正则表达式正确匹配(使用http://www.regular-expressions.info/javascriptexample.html进行测试),但是没有任何反应。 I've tried a similar rule, using RedirectMatch 301 instead: 我尝试了类似的规则,而是使用RedirectMatch 301:

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

And the URL transforms correctly, but leaves a "?" URL正确转换,但留下一个“?” at the end. 在末尾。 If I remove the question mark from that line, it transforms the URL correctly, but re-appends a query string on the end. 如果我从该行中删除问号,它将正确地转换URL,但最后会重新添加查询字符串。 Here's what I'm working with in .htaccess: 这是我在.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

Also, I'm using Drupal and what seems to be an older version of Apache (the QSD flag breaks my .htaccess, for example). 另外,我使用的是Drupal,并且它似乎是Apache的旧版本(例如,QSD标志破坏了我的.htaccess)。 I'm pulling my hair out and I hope that there's something really simple that I'm missing here. 我正在拔头发,希望这里真的很简单。

Try this: 尝试这个:

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]

Assuming you're using Apache httpd 2.4, you can add the [QSD] tag to the end of your RewriteRule to remove (Discard) the Query String. 假设您正在使用Apache httpd 2.4,则可以将[QSD]标记添加到RewriteRule的末尾以删除(丢弃)查询字符串。 If you're using 2.2 or earlier, the [QSD] flag isn't available. 如果您使用的是2.2或更早版本,则[QSD]标志不可用。 However, that trailing ? 但是,那尾随? doesn't actually hurt anything, and most website users never look at the URL anyway. 实际上并没有伤害任何东西,并且大多数网站用户无论如何也不会查看该URL。 If you're doing this for imaginary "SEO" benefits, you're almost certainly wasting time for no actual real benefit. 如果您这样做是为了获得假想的“ SEO”收益,那么您几乎肯定会浪费时间,而没有实际的实际收益。

Note also that you don't need to (and shouldn't) escape characters in the target of a RewriteRule. 还要注意,您不需要(也不应该)在RewriteRule的目标中转义字符。 Thus: 从而:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM