lolbas
lolbas

Reputation: 794

mod_rewrite - force redirecting to rewritten URL

I have following URL [1]:

www.domain.com/?search=somequery

which i want to redirect to [2]

www.domain.com/search/somequery

I am using following code in my .htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/(.*)$ /?search=$1 [L]

Code above works kind of fine since server understands both URLs: [1] and [2]. However i cannot figure out on how can i force redirect to [2] URL if the user visits [1] URL

Upvotes: 2

Views: 62

Answers (2)

anubhava
anubhava

Reputation: 785146

You need a new rule for that redirect:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /(?:index\.php)?\?search=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/(.*)$ /?search=$1 [L,QSA]

Upvotes: 1

Panama Jack
Panama Jack

Reputation: 24448

Try this

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /\?search=([^&\ ]+)
RewriteRule ^ search/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/(.*)$ /?search=$1 [L]

Upvotes: 3

Related Questions