.htaccess Rewrite URL with a query string

后端 未结 1 1721
温柔的废话
温柔的废话 2021-01-14 04:57

I have tried a ton of solutions posted at stackoverflow but nothing seems to work for me. I would like to rewrite a few URLs on my site to be search engine friendly.

相关标签:
1条回答
  • 2021-01-14 05:45

    UPDATED

    Now is tested. Try this:

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9-=_?]+)/?$ index.php?filter=$1 [L] 
    

    .htaccess goes in root directory.

    The requested URL is http://www.mysite.com/accounts/ but could be anything: http://www.mysite.com/apples/.

    NOTE: The URL displayed in the address bar is the one that was entered in that address bar. I assume is not http://www.mysite.com/index.php?filter=accounts because I guess the purpose of the redirection is to replace it with a more friendly one (http://www.mysite.com/accounts), which is the one that has to be typed in the address bar, not the other way around. Little confusing but I hope it makes sense.

    To test it, include the following only code at index.php in root directory.

    <?php 
    
    if ($_GET['filter'] == 'accounts') {
    echo "This is Accounts<br /><br />";    
    }else {
    echo "This is INDEX<br /><br />";       
    }
    
    ?>
    

    As to where to place the rewrite rules inside .htaccess, I could not know. I guess you have to try it before and after the actual rules.

    0 讨论(0)
提交回复
热议问题