Search code examples
.htaccessmod-rewriteurl-rewritingslash

htaccess rewrite, url doesn't work when i add a / at the end


I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.

i've got the following line in my htaccess (root map)

RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1

When i enter the the following url it works without a problem

www.myexample.com/page1/variable

the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.

Thanks for your time and help!


Solution

  • Get rid of the ending /$ sign on the first rule

    RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
    

    Or you can continue to capture data

    RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
    

    Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing

    RewriteRule ^page1/(.*)$ page1.php?url=$1
    

    Then use the server side script to determine what to do.