apache - Trailing Slash 301 Redirect Issue When ? Is In URL -
htaccess | 301 redirect | uri issue
i applied code htaccess enforce trailing slash on url:
#trailing backslash rewritebase / rewritecond %{http_host} !^www\. [nc] rewriterule ^(.*)$ https://www.example.com/$1 [r=301,l] rewritecond %{request_filename} !-f rewritecond %{request_uri} !(.*)/$ rewriterule ^(.*)$ $1/ [l,r=301]
the code works great @ adding / @ end of urls except ? in middle of url. below:
https://www.example.com/list-of-all-objects/search?keywords=test
this url code stated above change on load this: https://www.example.com/list-of-all-objects/search/?keywords=test
when should this: https://www.example.com/list-of-all-objects/search?keywords=test/
i have learnt of code have applied not work 2 reasons.
- they don't enforce / - make possible if blocked
- the term request_uri or $1 or $2 don't include ?keyword= (in fact there nothing can find refers part of url can't include correctly)
no matter - can't ? or after placed before /. how trailing slash not applied before ? on keyword search @ end of url , continue applied @ end of other url's?
other tried methods:
# force trailing slash enter code hererewritecond %{request_filename} !-f rewriterule ^(.*)([^/])$ http://%{http_host}/$1$2/ [l,r=301]
found here: .htaccess rewrite force trailing slash @ end
rewriteengine on rewritecond %{request_filename} !-f rewriterule ^(.*[^/])$ /$1/ [l,r=301]
found here: htaccess: add/remove trailing slash url
<ifmodule mod_rewrite.c> rewritecond %{request_uri} /+[^\.]+$ rewriterule ^(.+[^/])$ %{request_uri}/ [r=301,l] </ifmodule>
found here: http://www.paulund.co.uk/using-htaccess-to-force-trailing-slash
rewriteengine on rewritebase / rewritecond %{request_filename} !-f rewritecond %{request_uri} !(.*)/$ rewriterule ^(.*)$ http://%{http_host}/$1/ [l,r=301]
found here: http://ralphvanderpauw.com/seo/how-to-301-redirect-a-trailing-slash-in-htaccess/
rewriteengine on rewritebase / rewritecond %{request_filename} !-f rewritecond %{request_uri} !example.php rewritecond %{request_uri} !(.*)/$ rewriterule ^(.*)$ http://domain.com/$1/ [l,r=301]
found here: http://enarion.net/web/htaccess/trailing-slash/
# trailing slash check # don't fix direct file links rewritecond %{request_filename} !-f rewritecond %{request_uri} !(.*)/$ rewriterule ^(.*)$ $1/ [l,r=301]
found here: https://moz.com/community/q/trailing-slash-at-end-of-url
# append trailing slash rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewritecond %{request_uri} !/$ rewriterule . %{request_uri}/ [r=301,l]
found here: https://craftcms.stackexchange.com/questions/9501/force-trailing-slash-on-urls
below complete htaccess file contents:
rewriteengine on #trailing backslash rewritebase / rewritecond %{http_host} !^www\. [nc] rewriterule ^(.*)$ https://www.example.com/$1 [r=301,l] rewritecond %{request_filename} !-f rewritecond %{request_uri} !(.*)/$ rewriterule ^(.*)$ $1/ [l,r=301] #https redirects rewritecond %{https} off rewriterule ^ https://%{http_host}%{request_uri} [l,r=301] rewritecond %{http_host} !^www\. rewriterule ^ https://www.%{http_host}%{request_uri} [l,r=301] rewritecond $1 !\.(gif|jpe?g|png)$ [nc] rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule ^(.*)$ /index.php?/$1 [l] rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewriterule .* [qsa,l] <ifmodule mod_expires.c> # enable expirations expiresactive on # default directive expiresdefault "access plus 1 month" # favicon expiresbytype image/x-icon "access plus 1 year" # images expiresbytype image/gif "access plus 1 month" expiresbytype image/png "access plus 1 month" expiresbytype image/jpg "access plus 1 month" expiresbytype image/jpeg "access plus 1 month" # css expiresbytype text/css "access plus 1 month" # javascript expiresbytype application/javascript "access plus 1 year" </ifmodule>
query_string
not part of match in rewriterule.
to add trailing slash query strings, can use :
rewritecond %{query_string} ([^/]+)$ [nc] rewriterule ^ %{request_uri}?%1/ [r,l]
edit :
as said, query strings can not handled directly rewriterule directive, need separate rule add trailing slash query strings.
the example bellow adds trailing slash both parts of url.
rewriteengine on #add trailing slash uri rewritecond %{request_uri} !/$ rewriterule ^ %{request_uri}/ [nc,l,r] #add trailing slash query strings rewritecond %{query_string} ([^/]+)$ [nc] rewriterule ^ %{request_uri}?%1/ [r,l]
this change following urls :
to
or
to
(hope, helps!)
Comments
Post a Comment