Search code examples
wordpress.htaccess

Remove / sign from url


I have spent 2hours online trying to get htaccess that will work for this, I want to redirect all example.com/?s=keyword/ to example.com/?s=keyword

I want to remove the / Sign from the Url using .htaccess Please any help


Solution

  • Since the trailing slash appears on the query string portion of the URL you need to use mod_rewrite and check against the QUERY_STRING server variable. The RewriteRule (and Redirect, RedirectMatch) directives match against the URL-path only.

    (The linked questions deal with the trailing slash on the URL-path, not the query string - this is a different problem.)

    Try the following, at the very top of your .htaccess file, before any existing WordPress directives (ie. before the # BEGIN WordPress):

    RewriteCond %{QUERY_STRING} ^s=(\w+)/$
    RewriteRule ^$ /?s=%1 [R,L]
    

    (\w+) - This matches a single keyword of 1 or more "word" characters (a-z, A-Z, 0-9, _). The %1 backreference in the substitution string contains this keyword.

    No need to repeat the RewriteEngine On directive if this already exists in the WordPress block.

    Aside: I do wonder why you would need to do this? Since this is part of the s URL parameter, this can (and should) already be handled by the PHP code?


    UPDATE#1:

    Your code works but if there is spaces or - or + sign on the url, it will not redirect

    Spaces can be encoded as %20 or + in the query string. Providing you are OK with simply allowing %, rather than restricting this to specific URL encoded char(s), then you can modify the directives to read:

    RewriteCond %{QUERY_STRING} ^s=([\w%+-]+)/$
    RewriteRule ^$ /?s=%1 [NE,R,L]
    

    The NE (noescape) flag on the RewriteRule directive is required to prevent Apache from URL encoding the % (should it be present on the request) as %25 (in the substitution). Essentially preventing Apache from doubly encoding the output URL. We assume that if there is already a % on the requested URL, it is already part of a valid URL-encoded sequence, eg. %20 in the case of a space.

    To allow anything then change the RewriteCond directive to read:

    RewriteCond %{QUERY_STRING} ^s=(.+)/$
    

    Note that this assumes that there is only a single URL parameter on this URL (as in your example).

    Note also, that these are currently 302 (temporary) redirects. If this should be permanent then change R to R=301, but only once you have confirmed that it is working OK (to avoid caching issues).


    UPDATE#2:

    what if I want to redirect a folder e.g example.com/stack/?s=keyword/ to example.com/stack/?s=keyword

    To redirect a specific subdirectory (or path-segment) as in this example then modify the RewriteRule directive to something like:

    RewriteRule ^stack/$ /$0?s=%1 [NE,R,L]
    

    If you needed to match either the root directory or this subdirectory then use:

    RewriteRule ^(stack/)?$ /$1?s=%1 [NE,R,L]
    

    To match any subdirectory or the root directory then use something like the following:

    RewriteRule ^([\w-]+/)?$ /$1?s=%1 [NE,R,L]
    

    Note that, in this example, the subdirectory is limited to "word" characters plus - (hyphen).