【问题标题】:.htaccess redirect not working with query URL.htaccess 重定向不适用于查询 URL
【发布时间】:2018-08-18 10:11:05
【问题描述】:

我正在尝试进行从http://example.com/profile/{username}http://example.com/profile?username={username} 的重定向。我看到this question,但是当我尝试解决方案时,它会将主页重定向到:

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]

据我所知,这应该将请求重定向到profile 目录的子目录到配置文件目录。为什么这不起作用?

更新:

$_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
)

没有显示重定向,这是怎么回事?

【问题讨论】:

  • 试试这个 RewriteRule ^profile/(.+)$ /profile/?user=$1
  • 它不起作用,因为 profile/?user... 是相对链接,但 /profile/?user... 是绝对链接。 mod_rewrite 很难使用,比如 regexp :)

标签: .htaccess url-redirection


【解决方案1】:

你不应该使用R 标志,你的规则应该是这样的:

Options -MultiViews
RewriteEngine On

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

确保清除浏览器缓存或使用新的浏览器进行测试。

【讨论】:

  • 谢谢!我很确定我的东西是您回答另一个问题的(经过大量修改的)版本。虽然不能接受 7 分钟:P
  • 仍然将主页重定向到该 URL。尝试打印 $_SERVER 变量,请参阅更新后的问题。
  • 您是否在新浏览器中进行了测试?确保您没有任何其他规则或任何其他 .htaccess
  • 在隐身窗口和 Edge (urgh) 中对其进行了测试,它可以工作,但我现在尝试清除 cookie 几次,但它在 Chrome 中不起作用。尝试关闭和重新打开,没有。
  • 好的,清除缓存(误读您的答案),它现在可以工作了!谢谢!
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 2014-10-28
  • 1970-01-01
  • 2014-08-24
  • 2018-01-29
  • 1970-01-01
相关资源
最近更新 更多