2

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?

yaakov
  • 4,568
  • 4
  • 27
  • 51
  • try this RewriteRule ^profile/(.+)$ /profile/?user=$1 – toor Mar 09 '18 at 19:11
  • It isn't working because profile/?user... is relative link, but /profile/?user... is absolute link. mod_rewrite is very difficult to using, like regexp :) – toor Mar 09 '18 at 19:19

1 Answers1

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Thanks! I'm pretty sure my stuff was a (heavily modified) version of what you answered the other question with. Can't accept for 7 minutes though :P – yaakov Mar 09 '18 at 19:13
  • Still redirecting the homepage to that URL though. Tried printing the `$_SERVER` variable, see updated question. – yaakov Mar 09 '18 at 19:26
  • Did you test in a new browser? Make sure you don't have any other rule or any other .htaccess – anubhava Mar 09 '18 at 19:29
  • Tested it in an incognito window and in Edge (urgh) and it works, but I've tried clearing cookies a few times now, and it's not working in Chrome. Tried closing and reopening, nothing. – yaakov Mar 09 '18 at 19:31
  • 1
    Ok, cleared the cache (misread your answer), it's working now! Thanks! – yaakov Mar 09 '18 at 19:33
  • Is there a way I can allow for using a period in the username (e.g. joe.smith)? I tried adding a period to the character set `[\w-]` to `[\w.-]` but it gave me a 500 server error. – yaakov Mar 11 '18 at 06:08
  • Sorry to respond late as I was traveling. Try my updated rule now. Btw is that the reason of un-accepting the answer? – anubhava Mar 11 '18 at 19:00
  • Yes, it is indeed. – yaakov Mar 11 '18 at 20:33