Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querystring of internal rewrite shows up in redirect

I am using following htaccess

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
    RewriteRule ^user/([^/]*)/$ /user/index.php?usr=$1

The First three lines redirects non www url to www for my website..

The other line redirects for canonical purpose e.g. www.mysite.com/user/index.php?usr=JAHAJEE will become www.mysite.com/user/JAHAJEE/.

My problem is after i have added the above redirect for non www to www, the canonical redirected pages show the url parameter (e.g.www.mysite.com/user/JAHAJEE/?usr=JAHAJEE) . Please check http://www.jahajee.com/user/JAHAJEE/ & http://jahajee.com/user/JAHAJEE/

How can I prevent url parameters to be shown in the redirected URL. Thankyou in advance.

like image 919
jahajee.com Avatar asked Mar 22 '23 23:03

jahajee.com


1 Answers

The [R=301] will let mod_rewrite know the request should be redirected, but the request will not be redirected instantly. It will evaluate rules until it finds the [L] or [END] flag (or it hits the end of the file). It now matches the first rule, and tell that when mod_rewrite feels ready, it should redirect. mod_rewrite still hasn't found an L or END flag yet and still has rules to match. It then will match the second rule and include that in the redirect.

You need to change the flags for the first rule to [R=301,L] to make it redirect. The redirected request will invoke this .htaccess again and the second rule will be matched if needed. It then will work properly as an internal rewrite again.

like image 158
Sumurai8 Avatar answered Mar 29 '23 22:03

Sumurai8