Search code examples
html.htaccessurl-rewritingmamp

HTML URL Path Rewriting


I am running static HTML files on a local MAMP server and I would like to pass an ID to a page (user.html) in the 'URL Path' format. Here's what I need it to look like:

http://localhost:8888/mysite/user/3245653/john

  • user is the html page. I removed the .html extension from the url by using .htaccess rules
  • the number after the user/ is the ID parameter
  • john is another parameter I need to add to the url

This is what my .htaccess file looks like:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Currently the URL looks like this:

http://localhost:8888/mysite/user

So I could send parameters as Query strings using ?id= but I need to use the Path format

What is the change I need to make to create that URL Format?


Solution

  • You need add new rewrite rules to pass additional parameters to your pages:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^([^/]+)/?$ $1.html [L]
    
    RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.html?id=$1&name=$3 [L,QSA]
    
    RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.html?id=$1 [L,QSA]