Rewriting Dynamic URLs

I am trying to rewrite 1 dynamic URL to another dynamic URL as follows:

/category?category=News to /categorysearch/result/?q=news 

I was wondering if anyone has an idea on how this can be done in htaccess?

Thanks.

+7
source share
3 answers

So the final EDITED solution (for your related .htaccess):

 RewriteCond %{REQUEST_FILENAME} ethical [NC] RewriteCond %{QUERY_STRING} (^|&|%26|%20)ethical(=|%3D)([^&]+) [NC] RewriteRule .* /catalogsearch/result/?q=%3 [L,R] 

If I use category terms, as in this current question:

 RewriteCond %{REQUEST_FILENAME} category [NC] RewriteCond %{QUERY_STRING} (^|&|%26|%20)category(=|%3D)([^&]+) [NC] RewriteRule .* /categorysearch/result/?q=%3 [L,R] 

But the first one is clearer, the category of words is not used everywhere. If you want to add a string conversion, we will even do:

 # outside of a Directory section RewriteMap lowercase int:tolower # in a Directory section (not a .htaccess) RewriteCond %{REQUEST_FILENAME} category [NC] RewriteCond %{QUERY_STRING} (^|&|%26|%20)category(=|%3D)([^&]+) [NC] RewriteRule .* /catalogsearch/result/?q=${lowercase:%3} [L,R] 

But, as he commented , this last one with tolower could not work in .htaccess - anyway .htaccess is bad, indeed, for presentations you really need to think about setting up AllowOverride None and moving it all to VirtualHost. And nice things like rewriteMaps can't work with .htaccess files.

So now I will explain the rules. First, the main problem in your situation is that everything after the "?" is query_string , not the requested file name . And most rewriteRules try to work with the requested file name. So I first check that we are on the target requested file name:

 RewriteCond %{REQUEST_FILENAME} category [NC] 

Then we will work on the Query line (everything after the question mark). We could write something simpler:

 RewriteCond %{REQUEST_FILENAME} category [NC] RewriteCond %{QUERY_STRING} ^category=(.+) [NC] RewriteRule .* /catalogsearch/result/?q=${lowercase:%1} [L,R] 

Catch everything after? category = in variable% 1 available for RewriteRule. But the QUERY_STRING parameter has several problems, let's try to have fun with it:

  • decryption url not yet performed on this parameter
  • category may not be the first parameter in the parameter list
  • maybe not even the last (& foo = bar)
  • you may have spaces before

EDITED

My first answer was:

 RewriteCond %{REQUEST_FILENAME} category [NC] RewriteCond %{QUERY_STRING} [^|&|%26|%20][category](=|%3D)([^&]+) [NC] RewriteRule .* /catalogsearch/result/?q=${lowercase:%2} [L,R] 

With [^|&|%26|%20] catch the fact that it can be the initial parameter or parameter after a space and / or space, %26 is also urlencoded.

But that was wrong , thanks to the comment by @mootinator. I checked a little more, [category] matches the category, but also the "atcatcategory" and any combination of these letters.

 RewriteCond %{QUERY_STRING} category(=|%3D)([^&]+) [NC] 

Matches the argument to 'category', removing ^ allows this argument to fit anywhere. But it also matches the argument named subcategory . Thus, the first letter must be a space or (or the beginning of a query string).

This caused [^|&|%26|%20] , which meant for me the beginning of the OR chain and / or space or in urlencoded. But that was also not true , ^ inside [] means negation. Therefore, we should also use a suitable bracket here: (^|&|%26|%20) , now it works, the only problem is that the value of the argument is now% 3, not% 2

Then we catch the word (category), to be really accurate, we should actually catch it after the letters with uppercase, lowercase and uppercase letters, something like awfull [c|C|%43|%63][a|A|%61|%41][t|T|%74|...]to be continued - and I should probably use brackets instead of brackets, hell, check the list of characters with urlencoded here .

(=|%3D) is the character '=' urlencoded or not. I could use [=|%3D] , but %3D not suitable in this case, I do not understand why (mod_rewrite is a country of strange things) . Because of this first matching bracket, we will have to use the variable% 2, not% 1.

And then we have a nice ([^&]+) that means anything but not "&".

After these conditions we will apply RewriteRule. We take everything (this .* ) And redirect it to /catalogsearch/result/?q=%2 , where we use %3 , not $1 or $3 , since we are not fixing anything in rewriteRule, but in the state up to ( %3 this is the third captured information about the last condition, therefore it is ([^&]+) ). If you add the QSA flag ([L, R, QSA]) to the rewriteRule, you will even return other parameters in the final query string so that:

 ethical?bar=toto& ethical=Foo&zorglub=titi => is redirected to catalogsearch/result/?q=foo&bar=toto&%2520ethical=Foo&zorglub=titi 

And if I play with a little URL encoding:

 ethical?bar=toto%26 ethical%3DFoo&zorglub=titi => is redirected to catalogsearch/result/?q=foo&bar=toto%2526%2520ethical%253DFoo&zorglub=titi 

But this is not the end of the game, I will let you deal with all the problems associated with urlencoding that you may encounter, so that you can detect all the variations by the value of Foo and mix them with tolower (here it is broken). You might not even have problems with encoded URLs in parameters or additional attributes, but it was fun :-)

+5
source

There are two options, depending on what you are trying to do and how specific things should be.

Rob W's first solution is to redirect the user from /category?category=News to /categorysearch/result/?q=News

If, as you see, your URLs are case sensitive, you can look at the PHP script as follows:

 $matches = array(); if( preg_match( "/^/category\?category=(.+)/", $_SERVER['REQUEST_URI'], $matches ) ) { header( 'Location: http://' . $_SERVER['HTTP_HOST'] . '/categorysearch/result/?q= . strtolower($matches[1]) ); } else { //ERROR HANDLING } 

After that, you can use a rule like the following to redirect using a fully string parameter.

 RewriteRule ^category\?category=(.+)$ redirect.php [L] 

By combining this solution, redirecting /category?category=News to /categorysearch/result/?q=News

0
source

I think,

RewriteRule ^/category?category=(.*) /categorysearch/result/?q=news

Let's do it.

-one
source

All Articles