301 Htaccess RewriteRule Query_String

Problem: Visitors open the URL of website.com/?i=133r534|213213|12312312 , but this URL is no longer valid and must be redirected to website.com/#Videos:133r534|213213|12312312

What I tried: Over the past hours I have tried many mod_rewrite (.htaccess) rules using Query_String, all failed. The last post in this section shows a solution to this problem, but what would be the rule in my situation.

I am very curious how you would solve this problem :)!

+3
source share
1 answer

Below is a simple case that you are showing. You will need to add additional logic if you need to allow other parameters in the query string or file names before ?.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^i=(.*)
RewriteRule ^.*  /#Video:%1? [NE,R=permanent]

Why is it difficult?

  • RewriteRule does not look at the query string, so you need to use RewriteCond to evaluate the QUERY_STRING variable and capture the part that you will need later (link through% 1)
  • the hash symbol (#) is usually escaped, you must specify the [NE] flag
  • The end? per lookup string is required to suppress the original query string

I tested this on Apache 2.2.

+6
source

Source: https://habr.com/ru/post/1767081/


All Articles