Force redirect permanently on RedirectMatch from $_GET url to semantic url

Viewed 44

I am doing some .htaccess settings to improve SEO with semantic URL's. Currently when you go to the semantic URL www.website.com/profile/exampleusername it works perfectly although i am still able to access the same page without any red alarms rining with the pure get request in the url. www.website.com/profile?username=exampleusername. I have tried to user both the redirect and RedirectMatch but i can't get it to work.

Here is the relevant code from the .htaccess

RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+)&buddy_identifier=([^\s&]+) [NC]
RewriteRule ^profile/%1/%2? [R=302,L]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ profile.php?username=$1&buddy_identifier=$2 [L,NC,QSA]

I want to be able to go to www.website.com/profile?username=exampleusername and then be redirected to www.website.com/profile/exampleusername

Full .htacces

RewriteEngine on
Options -MultiViews +FollowSymLinks -Indexes

    RewriteCond %{ENV:REDIRECT_STATUS} ^$

        ############# REMOVE FILE EXTENSION #############

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

    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,NE,L]


    #####

    ######## ONLY ACCESS BY HTTPS://WWW.* AND NO EXTENSION #############
        #commented bcs of dev workspace localhost

    #RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
    #RewriteRule ^ /%2 [L,R=301]
    #RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

#RewriteCond %{HTTPS} off [OR]
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
#RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]


############# INDEX RENAME #############

DirectoryIndex gamingbuddy.php


################### SEMANTIC URL ######################

### URL FOR GAMES.PHP ###

RewriteCond %{THE_REQUEST} /games\.php\?game=([^\s&]+)&game_style=([^\s&]+) [NC]
RewriteRule ^games/%1/%2? [R=302,L]
RewriteRule ^games/([^/]+)/([^/]+)/?$ games.php?game=$1&game_style=$2 [L,NC,QSA]


### URL FOR PRIVATE PROFILE ###

RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+)&buddy_identifier=([^\s&]+)\s [NC]
RewriteRule ^profile/%1/%2? [R=302,L]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ profile.php?username=$1&buddy_identifier=$2 [L,NC,QSA]
1 Answers

You can use these rules:

Options -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} /profile(?:\.php)?\?username=([^\s&]+)\s [NC]
RewriteRule ^ /profile/%1? [R=302,L]

RewriteRule ^profile/([^/]+)/?$ profile.php?username=$1 [L,NC,QSA]
Related