简体   繁体   English

htaccess重定向查询字符串+强制重定向

[英]Htaccess redirect query string + force redirect

Here is the deal I got this string with 2 OPTIONAL parameters 这是我得到带有2个OPTIONAL参数的字符串的交易

/index.php?lang=en&p=about

it could be both ways 可能是两种方式

/index.php?p=about&lang=bg

and the p parameter is optional it could go without the p or the lang one 并且p参数是可选的,它可以不带plang一个

So far I was able to redirect one of them from (1) /index.php?p=about to (2) /about what it does not do and I could only force it the other way around is to redirect the (1) to (2) . 到目前为止,我已经能够将其中之一从(1) /index.php?p=about(2) /about它不执行的操作,而我只能用另一种方法强制它重定向(1)(2) When you type (1) to redirect you to (2) . 当您键入(1)将您重定向到(2)时 I did it in reverse using [R] .. 我用[R] ..做的相反。

I am really bald in this htaccess patterns and stuff, so the harder question is about the 2nd query lang as your presume it's about changing the language so I gotta make it like 我对htaccess模式和东西真的很秃头,所以更棘手的问题是关于第二查询lang因为您假设它是关于更改语言的,所以我必须做到

From /index.php?lang=en&p=about to /en/about , but also the /index.php?p=about&lang=en to lead to /en/about and those redirects to be forced when you type the whole query to be redirected to the seo friendly version. /index.php?lang=en&p=about/en/about ,还有/index.php?p=about&lang=en导致/en/about ,当您将整个查询键入到时,这些重定向将被强制执行重定向到seo友好版本。

Also when you don't have lang one it should go for the default en 另外,当您没有lang也应该使用默认的en

I am testing right now various sample redirects, but so far nothing is working as it should. 我现在正在测试各种示例重定向,但是到目前为止,没有任何事情可以正常工作。

You can use this rule that will support query parameters in any order: 您可以使用以下规则以任何顺序支持查询参数:

RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING}     (?:^|&)lang=([^&]*)
RewriteCond %1::%{QUERY_STRING} (.*?)::(?:|.*&)p=([^&]*)
RewriteRule ^(index\.php)?$ /%1/%2? [L,NC,R=302]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/(\w+)/?$ index.php?lang=$1&p=$2 [L,QSA]

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

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