Search code examples
.htaccessmod-rewriteurl-rewritingfriendly-urlpretty-urls

How to create pretty url using htaccess


I have a link like http://example.com/file.php?id=7gJKw2&d=78sfmnnsd8

I want to make this above url as

http://example.com/file/7gJKw2/78sfmnnsd8

Here is what I've tried

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

How can i add the value of d & make it good looking?


Solution

  • You can bundle the entire a-zA-Z0-9_ pattern into a simple \w. As to the real question, just add the second part as follows:

    RewriteRule ^file/([\w.-]+)/([\w.-]+)/?$ file.php?id=$1&d=$2 [L]
    RewriteRule ^file/([\w.-]+)/?$ file.php?id=$1 [L]