Search code examples
php.htaccessurlslug

Issue with URL slug.


I can't seem to obtain the second part of the url slug. I currently have my .htaccess file like this:

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

And get this:

http://example.com/user/john

And now can't get this to work:

 http://example.com/user/john?o=last

How can I obtain $_GET['o']?


Solution

    1. You need QSA (Query String Append) flag to preserve existing queries while redirecting.
    2. You don't need 2 rules just for handling a trailing slash.

    Replace your code with this:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([a-z0-9_-]+)/?$ /index.php?id=$1 [L,QSA,NC]