简体   繁体   English

.htaccess drupal 7 RedirectMatch 或 RewriteRule 没有接缝工作

[英].htaccess drupal 7 RedirectMatch or RewriteRule nothing seams to work

I'm having some real issues getting a redirect to work in .htaccess with drupal 7.我在使用.htaccess和 drupal 7 重定向时遇到了一些实际问题。

I have links like this:我有这样的链接:

example.com/vid/category/filter?subcat_filter_depth=125

and I need them to go to: (on same domain)我需要他们 go 到:(在同一个域上)

example.com/vid/category/filter?type=All&subcat_filter_depth=125

after = the number may be different like 67 or 32 etc. meaning that can be any number and whatever number is at the end of the original link needs to also be at the end of the new link. after =数字可能不同,如 67 或 32 等。这意味着可以是任何数字,原始链接末尾的任何数字也需要位于新链接的末尾。

I think the fact that the link is pretty much the same path just different filter is what is causing the issue as even the Drupal redirect url module will not allow it at an attempt to add them one at a time as they occur.我认为链接几乎是相同的路径只是不同的过滤器这一事实是导致问题的原因,因为即使 Drupal 重定向 url 模块也不允许它在它们发生时一次添加一个。

here is everything I've tried:这是我尝试过的一切:

RedirectMatch 301 ^/vid/category/filter?subcat_filter_depth=(.*) /vid/category/filter?type=All&subcat_filter_depth=$1

RewriteRule ^(.*)/vid/category/filter?subcat_filter_depth=(.*) $1/vid/category/filter?type=All&subcat_filter_depth=$2 [R=301]

RedirectMatch ^/vid/category/filter?subcat_filter_depth=(.*) ^/vid/category/filter?type=All&subcat_filter_depth=$1

RedirectMatch 301 ^(.*)/filter?subcat_filter_depth=(.*) ^$1/filter?type=All&subcat_filter_depth=$2

RedirectMatch ^/(.*)/filter?subcat_filter_depth=(.*) ^/$1/filter?type=All&subcat_filter_depth=$2

RewriteRule "^/(.*)/filter?subcat_filter_depth=(.*)" "^/$1/filter?type=All&subcat_filter_depth=$2"

mod_rewrite is on and there is an AllowOverride pathed/to/the/.htaccess file under <Directory> inside <VirtualHost> in the 000-default.conf running on Debian Apache..pretty much latest versions. mod_rewrite 已打开,在000-default.conf<VirtualHost>内的<Directory>下有一个 AllowOverride pathed/to/the/.htaccess 文件运行于 Debian Apache..pretly 最新版本。

I know mod_rewrite is working because if I add a bad rule or rewrite with a syntax error I get error 500 response page.我知道 mod_rewrite 正在工作,因为如果我添加错误规则或重写语法错误,我会收到错误 500 响应页面。

I've searched and tried just about everything I can think of and nothing seems to work.我已经搜索并尝试了几乎所有我能想到的东西,但似乎没有任何效果。

Any help getting this sorted and functioning would be greatly appreciated!非常感谢任何对此进行排序和运行的帮助!

Both the RedirectMatch (mod_alias) and RewriteRule (mod_rewrite) directives match against the URL-path only, which notably excludes the query string . RedirectMatch (mod_alias) 和RewriteRule (mod_rewrite) 指令都只匹配 URL 路径,特别是排除了查询字符串

To match against the query string you need to use RewriteRule with an additional condition ( RewriteCond directive) that checks against the QUERY_STRING server variable.要匹配查询字符串,您需要将RewriteRule与附加条件RewriteCond指令)一起使用,以检查QUERY_STRING服务器变量。

This rule also needs to go near the top of the .htaccess file, before any existing rewrites.在任何现有重写之前,此规则还需要 go 靠近.htaccess文件的顶部。

For example:例如:

RewriteCond %{QUERY_STRING} ^subcat_filter_depth=\d+$
RewriteRule ^vid/category/filter$ /$0?type=All [QSA,R=302,L]

This redirects example.com/vid/category/filter?subcat_filter_depth=<number> to example.com/vid/category/filter?type=All&subcat_filter_depth=<number> .这会将example.com/vid/category/filter?subcat_filter_depth=<number>重定向到example.com/vid/category/filter?type=All&subcat_filter_depth=<number>

In .htaccess there is no slash prefix on the URL-path matched by the RewriteRule pattern ..htaccess中,与RewriteRule模式匹配的 URL 路径上没有斜杠前缀。 ie. IE。 ^vid/category (not ^/vid/category ). ^vid/category (不是^/vid/category )。

$0 is a backreference that contains the matched URL-path from the RewriteRule pattern . $0是一个反向引用,其中包含来自RewriteRule pattern的匹配 URL 路径。

QSA causes the original query string (eg. subcat_filter_depth=125 ) to be appended to the query string stated in the substitution string, ie. QSA导致原始查询字符串(例如subcat_filter_depth=125 )附加到替换字符串中规定的查询字符串,即。 type=All becomes type=All&subcat_filter_depth=125 . type=All变为type=All&subcat_filter_depth=125

Test with 302 (temporary) redirect to avoid potential caching issues.使用 302(临时)重定向进行测试以避免潜在的缓存问题。 Only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed it works as intended.只有更改为 301(永久)重定向 - 如果这是意图 - 一旦您确认它按预期工作。

You will likely need to clear your browser cache before testing.在测试之前,您可能需要清除浏览器缓存。

I was able to come up with a solution that worked prior to the answer (above) I marked as correct as my solution isn't quite as elegant.我能够想出一个在答案(上图)之前有效的解决方案,我标记为正确,因为我的解决方案不是那么优雅。

  • I did realize after a bit of more research I needed to use a RewriteCond with %{QUERY_STRING} for what I was trying to achieve.经过更多研究后,我确实意识到我需要使用带有 %{QUERY_STRING} 的 RewriteCond 来实现我想要实现的目标。

  • Again not as elegant as the answer above...but here is my code:又不像上面的答案那样优雅......但这是我的代码:

RewriteCond %{REQUEST_URI} ^/vid/category/filter [NC]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)
RewriteRule (.*) /vid/category/filter?type=All&subcat_filter_depth=%2 [R=301,L]

This actual kept the url in the browser as it was input (/var/category/filter?subcat_filter_depth=) but showed the page as if it was opened with the proper filter for type=ALL and produced no errors both in browser and in Drupal dblog.这实际上在输入时将 url 保留在浏览器中 (/var/category/filter?subcat_filter_depth=),但显示页面就像是使用 type=ALL 的正确过滤器打开的一样,并且在浏览器和 Drupal 中都没有产生错误博客。

The above solution is more useful in explanation alone and gave me much better understanding of how to break up a URL and put it back together with modifications especially when dealing with strings that have variables that change like unique ID's.上面的解决方案单独解释更有用,让我更好地理解如何分解 URL 并将其与修改一起放回去,尤其是在处理具有像唯一 ID 一样变化的变量的字符串时。

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

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