Score:1

Rewrite Rule for URL with query does not work

cn flag

I write the following rewrite rule:

<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} rp=\/knowledgebase\/.*
RewriteRule ^\/customer\/index.php /knowledgebase/ [R=301,L]
RewriteRule ^\/customer\/knowledgebase\.php$ /knowledgebase/ [R=301,L,QSA]
</IfModule>

To redirect URL such as

https://www.example.com/customer/index.php?rp=/knowledgebase/5/DataNumen-Excel-Repair to https://www.example.com/knowledgebase/

And redirect URL such as

https://www.example.com/customer/knowledgebase.php to https://www.example.com/knowledgebase/

But both do not work. Why?

Update

I try to put MrWhite's codes from /.htaccess to /customer/.htaccess and make some minor changes to adopt the changes, as below:

RewriteCond %{QUERY_STRING} rp=/knowledgebase/
RewriteRule ^index\.php$ https://www.example.com/knowledgebase/ [QSD,R=301,L,NC]

RewriteRule ^knowledgebase\.php$ https://www.example.com/knowledgebase/ [R=301,L,NC]

Now the redirect works. However, it will only work for case like:

https://www.example.com/customer/index.php?rp=/knowledgebase/9/DataNumen-PDF-Repair

but for case like

https://www.example.com/customer/index.php?a=b&c=d&rp=/knowledgebase/9/DataNumen-PDF-Repair

It will not work. Even after I change ^rp= to rp in RewriteCond.

Score:1
kz flag

RewriteRule ^/customer/index.php /knowledgebase/ [R=301,L]

In .htaccess, the URL-path matched by the RewriteRule pattern (ie. ^\/customer\/index.php) does not start with a slash, so this will never match. The URL-path that is matched is relative to the directory that contains the .htaccess file (less the slash prefix). (Unlike when used in a server context, when the URL-path that is matched is the document-root-relative URL-path, starting with a slash.)

You will also need the QSD flag on the first rule to discard the query string on the original request, otherwise this is passed through as-is.

Minor point... there is no need to backslash-escape slashes in Apache regex, since spaces are the argument delimiter and the slash carries no special meaning otherwise in regex.

Try the following instead:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^rp=/knowledgebase/
RewriteRule ^customer/index\.php$ /knowledgebase/ [QSD,R=301,L]

RewriteRule ^customer/knowledgebase\.php$ /knowledgebase/ [R=301,L]

I anchored the query string in the condition so it matches rp=... at the start of the query string, as in your example. The .* at the end of the regex is not required.

The QSA flag is not required on the 2nd rule since the query string is passed through by default. The QSA flag would only be required if you needed to merge the original query string on the request with a new query string you were appending in the substitution.

The <IfModule> wrapper is not required, unless these directives are optional and intended to be used on multiple servers where mod_rewrite might not be available. See this question on the Webmasters stack: https://webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary

You should test first with 302 (temporary) redirects to avoid potential caching issues.


UPDATE: to make things simple, I put the rewrite rules to the .htaccess file under /customer instead of /. I have updated my post accordingly.

In that case, you would need to remove the customer/ prefix on each of the RewriteRule patterns, as you have done in your updated question.

If the rp URL parameter is intended to match anywhere in the query string then you should change the CondPattern to read (^|&)rp=/knowledgebase/, rather than simply removing the ^ prefix. By removing the ^ prefix you are potentially matching too much, eg. abcrp=/knowledgebase/... would also match, although that may or may not be an issue in reality.

Otherwise, those updated directives look OK, depending on any other conflicts that might exist with other directives. Caching might also be an issue, especially if you are using CDN.

alancc avatar
cn flag
Sorry but that still not working. Thank you very much.
kz flag
@alancc What do you mean by "not working" exactly? Do you get an error? Incorrect redirect? Nothing? Where is your `.htaccess` file located? Do you have any other directives? Any other `.htaccess` files?
alancc avatar
cn flag
@MrWrite, Thank you very much. "Not working" means when I input the URL such as https://www.example.com/customer/index.php?rp=/knowledgebase/9/DataNumen-PDF-Repair, then nothing happens. I check the response code and it shows 200. I guess that may be due to the multiple .htaccess files, so to make things simple, I put the rewrite rules to the .htaccess file under /customer instead of /. I have updated my post accordingly.
kz flag
@alancc The directives as written in my answer are intended to go in the root `.htaccess` file. If you put them in the `/customer` subdirectory then you will need to modify them as you have done in your updated question. The directives in your updated question look OK (having removed the `^` prefix to match the 2nd URL - although that now arguably matches too much). However, do you have any other directives in the `/customer/.htaccess` file? Any external redirects like this need to go near the top. Multiple `.htaccess` files can be problematic. Are you using rewrite _inheritance_ at all?
alancc avatar
cn flag
Thank you very much. I double check the /customer/.htaccess, after my rewrite rule, there is the AddHandler which is generated by cPanel automatically. And that is the last statement in the .htaccess. The actual URL is https://www.datanumen.com/customer/index.php?a=b&c=d&rp=/knowledgebase/9/DataNumen-PDF-Repair, though now Cloudflare is enabled, I have tried when it is disabled and also not working.
kz flag
@alancc I see that URL now appears to redirect as intended. What was done to resolve this? Was it a caching issue? "AddHandler which is generated by cPanel automatically" - cPanel should not be editing the `.htaccess` file in the subdirectory? `AddHandler` would ordinarily be in the root `.htaccess` file only. (?)
alancc avatar
cn flag
I guess that is due to the Cloudflare Cache. Though I pause Cloudflare on my site when I test, the cache or something may still inference. Now the cache has expired. Yes, normally Addhandler should be for root .htaccess. However, in my case, the customer uses WHMCS that usually not support the latest version of PHP, so I have to set a different version on that subdir. Thank you very much for pointing out this.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.