Search code examples
regexwordpress.htaccesshttp-redirecturl-rewriting

how do I use rewriteRule in htaccess


I'm trying to redirect some urls to pretty urls and the thing is that I'm new to this so I can't seem to figure out what part I did wrong this is the htaccess right now but the rewrite rule is not working.

   # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ -
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([.]+)/([.]+)$ index.php?search_categories%5B%5D=$1&search_location=$2 [QSA,L]

RewriteRule . /index.php [L] 

</IfModule>
# END WordPress

Solution

  • Try this rule:

    RewriteRule ^(.+[^/])/(.+)$ index.php?search_categories=$1&search_location=$2 [QSA,L]
    

    In your rule you have: search_categories%5B%5D, may be a mistake while copy/paste I removed the %5B%5D even this is not the problem

    ^(.+[^/])/(.+)$ match category/location the rule [^/] is to not match the slash /

    EDIT:

    It can also simplified with just: ^([^/]*)/(.*)$

    In your code [.]+ the dot match Any single character but brackets is used for range like [abc] will match a, b or c, so it will not work with just [.]+, also like I wrote before you have to not match the slash with [^/] this last one must be inside brackets, more here and Cheat Sheet

    Test here