Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

Use ReWriteEngine to redirect user profile page to a physical page


Good day,

I want to rewrite my user id URL to a physical page on my website, see URL structure below:

From: http://localhost:8888/report.mark1/user.php?id=341

To: http://localhost:8888/report.mark1/341

This is the .htaccess file code:

ReWriteEngine On
RewriteRule ^report.mark1([^/.]+)?$ /user.php?id=$1 [L]

I'm I missing something?


Solution

  • With your shown samples, please try following htaccess rules file. Make sure your .htaccess file is present alongside with report.mark1 folder and your user.php is present inside report.mark1 folder.

    Make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteBase /report.mark1/
    ##External redirect change of URL rules here.
    RewriteCond %{THE_REQUEST} \s/(report\.mark1)/user\.php\?id=(\d+)\s [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    
    ##Internal rewrite to user.php rules here.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(?:[^/]*)(\d+)/?$ /report.mark1/user.php?id=$1 [QSA,L]