强制在RedirectMatch上从$ _GET URL永久重定向到语义URL

时间:2018-11-12 20:53:49

标签: .htaccess

我正在做一些.htaccess设置来改善带有语义URL的SEO。当前,当您转到语义URL www.website.com/profile/exampleusername时,它可以正常运行,尽管我仍然能够访问同一页面,而不会在URL中使用纯get请求冲洗任何红色警报。 www.website.com/profile?username=exampleusername。 我已经尝试过同时使用redirect和RedirectMatch,但是我无法使其正常工作。

这是.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]

我希望能够转到www.website.com/profile?username=exampleusername,然后重定向到www.website.com/profile/exampleusername

完整的.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 个答案:

答案 0 :(得分:1)

您可以使用以下规则:

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]