php dynamic parameters to url with htaccess

Viewed 130

I'm trying to make SEO Friendly url by altering php parameters with slash based url.

I want to make
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20 into: mysite.com/ToplistName/2021-02-20/2021-02-20

I had success with rewriting url but none of my includes are referring to right directory path and I get no css, js and file paths are broken from links.

This is what I have in .htaccess file now:

RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ TopList.php?tl=$1&fr=$2&to=$3

Anyone that can help me get this sorted out inside .htaccess file?

2 Answers

Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(\d{4}-\d{2}-\d{2})/(\d{4}-\d{2}-\d{2})/?$  TopList.php?tl=$1&fr=$2&to=$3 [L]

I solved this by doing following:

  1. added to html:
    <base href="http://example.com/">

  2. added to .htaccess:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    #1 Changing Full query
    RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ TopList.php?tl=$1&fr=$2&to=$3
    #2 Changing only first landing page
    RewriteRule ^([^/.]+)/?$ TopList.php?tl=$1

this gave me following result:
mysite.com/TopList.php?tl=ToplistName ==> mysite.com/ToplistName
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20 ==> mysite.com/ToplistName/2021-02-10/2021-02-20

Related