2
votes

Here is my htaccess code:

RewriteEngine On
RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]

I need to make a rewrite rule like this:

http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term

OR if there is a category

http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category

I need also to redirect all the old urls to the new one.

With this rules all that I can obtain is to have the search term and category name joined togheter. In a nutshell it is as if I had always:
http://mywebsite.com/?search=query+term/Category
If I remove all the conditions (RewriteCond), leaving only the rewrite rule with the addition of a trailing slash:

RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]

Reaching the url of my interest in a direct way, the whole thing works. But I will not have the redirect..

So I have tried to set a rule like this:

RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]

But with this rule I get just a loop of redirects.

1

1 Answers

1
votes

You can use the following rules :

RewriteEngine on

#1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
RewriteRule ^ /s/%1/%/%2? [NC,L,R]
#2--Redirect from "/?search=foobar" to "/s/foobar" --#
RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
RewriteRule ^ /s/%1? [NC,L,R]
#1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
#2--Rewrite "s/foobar" to "/?search=foobar"--#
RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]