I have searched some on this site but couldn’t find a right answer to my question.
I am trying to force a www redirect and forcing an endslash on each url.
I have the following lines in my htaccess:
# enable rewriting
RewriteEngine on
# if not a file or folder, use index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
# force www
RewriteCond %{HTTP_HOST} !^www
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force endslash
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301]
My www redirect is working fine, it will go like:
http://example.com
to
http://www.example.com
But now the strange part is, my end slash line is adding the url parameter which I don’t want.
So this will go like:
http://www.example.com/path/without/endlash
to
http://www.example.com/index.php/?url=path/without/endslash
Why is it that my defined url parameter is used in this case, and how can I prevent this.
Thanks in advance
EDIT:
thanks to icrew
Only added no-file/no-dir before the endlash entry because otherwise it would redirect my assets.
Final code:
RewriteCond %{HTTP_HOST} !^www
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This part of .htaccess
is responsible for
http://www.example.com/index.php/?url=path/without/endslashredirection. So basicly if you do not want url parameter in query string remove those three lines.Edit: I figure out what you wanted. Bellow is correct code