Rewrite website URL to remove query string variables question mark?

Viewed 59

I have a current url that has query string variables. I'd like to remove them so that only the second part of the query string is visible.
From: mysite.com/posts?post=45&tab=profile
To: mysite.com/posts/45/profile
Current .htaccess file

RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+access/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/access/$1.php -f
RewriteRule ^(.+?)/?$ access/$1.php [L,NC]

SITEMAP

index.php
/access
    -dashboard.php
    -posts.php
    -account.php

This query string will always have parameter 1, but may or may not have parameter 2, depending on what they click on the scree.

1 Answers

Have it like this:

Options -MultiViews
RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+access/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# 2 parameter rule
RewriteCond %{DOCUMENT_ROOT}/access/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ access/$1.php?post=$2&tab=$3 [QSA,L]

# 1 parameter rule
RewriteCond %{DOCUMENT_ROOT}/access/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ access/$1.php?post=$2 [QSA,L]

# php extension
RewriteCond %{DOCUMENT_ROOT}/access/$1.php -f
RewriteRule ^(.+?)/?$ access/$1.php [L,NC]

# php extension
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L,NC]
Related