Rewrite URL with .htaccess for multiple parameters

前端 未结 1 975
长情又很酷
长情又很酷 2020-12-01 08:20

This question might be a duplicate. But I did not find any solution worked for me. I want to rewrite URL, where I have one and two level parameters. first parameter is

相关标签:
1条回答
  • 2020-12-01 09:13

    You have to treat the rules separately. All Conditions preceding rules only apply to a single rule. Following rules are not touched by peceding rule and their conditions. You tried to 'chain' two rules. The second rule never could have matched, since the first one was a catch-all that changed the syntax. Apart from that you have to make sure that the first rule does not catch unwanted requests. Also think about whether you want to use the * or the + operator in the patterns. I suggest you use the + operator, so that you have a clear error message when empty values are requested for a 'page' or a 'subpage'.

    So this might come closer to what you are looking for:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)$ index.php?p=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)$ index.php?p=$1&sp=$2 [L]
    
    0 讨论(0)
提交回复
热议问题