.htaccess redirect not working with query URL

时间:2018-03-09 19:06:09

标签: .htaccess url-redirection

I'm trying to make a redirect that goes from http://example.com/profile/{username} to http://example.com/profile?username={username}. I saw this question, but when I tried the solution, it redirected the homepage to:

http://example.com/profile/?user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=index.php

.HTACCESS

RewriteEngine On    

RewriteRule ^profile/(.+)/$ profile/?user=$1 [R,L,QSA,NC]

As far as I can tell, this is supposed to redirect requests to the subdirectories of the profile directory to the profile directory. Why isn't this working?

UPDATE:

$_SERVER

Array
(
    [MIBDIRS] => C:/xampp/php/extras/mibs
    [MYSQL_HOME] => \xampp\mysql\bin
    [OPENSSL_CONF] => C:/xampp/apache/bin/openssl.cnf
    [PHP_PEAR_SYSCONF_DIR] => \xampp\php
    [PHPRC] => \xampp\php
    [TMP] => \xampp\tmp
    [HTTP_HOST] => msk.local
    [HTTP_CONNECTION] => keep-alive
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    [HTTP_DNT] => 1
    [HTTP_REFERER] => http://msk.local/profile/?user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=index.php
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.9,la;q=0.8,lb;q=0.7
    [HTTP_COOKIE] => PHPSESSID=4gsjup44pb85q6bsvh5aam2bke
    [PATH] => C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\nodejs\;C:\Ruby24-x64\bin;C:\Users\yakai\AppData\Local\Microsoft\WindowsApps;C:\Users\yakai\AppData\Local\atom\bin;C:\Users\yakai\AppData\Roaming\npm
    [SystemRoot] => C:\Windows
    [COMSPEC] => C:\Windows\system32\cmd.exe
    [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.RB;.RBW
    [WINDIR] => C:\Windows
    [SERVER_SIGNATURE] => 
Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.1 Server at msk.local Port 80


    [SERVER_SOFTWARE] => Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.1
    [SERVER_NAME] => msk.local
    [SERVER_ADDR] => 127.0.0.1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 127.0.0.1
    [DOCUMENT_ROOT] => C:/xampp/htdocs/msk
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => C:/xampp/htdocs/msk
    [SERVER_ADMIN] => postmaster@localhost
    [SCRIPT_FILENAME] => C:/xampp/htdocs/msk/profile/index.php
    [REMOTE_PORT] => 64756
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=index.php
    [REQUEST_URI] => /profile/?user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=profile/&user=profile&user=index.php
    [SCRIPT_NAME] => /profile/index.php
    [PHP_SELF] => /profile/index.php
    [REQUEST_TIME_FLOAT] => 1520623498.877
    [REQUEST_TIME] => 1520623498
)

It's not showing that it was redirected, so what is going on?

1 个答案:

答案 0 :(得分:1)

You should not be using R flag and your rule should be like this:

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([^/]+)/?$ profile/?user=$1 [L,QSA,NC]

Make sure to clear browser cache or use a new browser for testing.