Rewrite url for the actual url in the address bar

I am having a weird problem where on some of the user's computers the rewritten url resolves their actual url in the address bar.

My htaccess file has the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

      

I noticed they showed up in my analytics, which messed up my stats as there are two URLs on the same page.

An example of this is:

http://example.com/news/the-first-post

http://example.com/index.php?url=news/the-first-post

I could sometimes replicate the problem myself by directly typing the rewritten url in the address bar.

+3


source to share


1 answer


You have these rules:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

      



Now all URLs with structure /index.php?url=news/the-first-post

will be 301 (persistent) redirects to /news/the-first-post

.

+1


source







All Articles