Laravel:重定向类型为`user.php?id = 3`的url

时间:2018-02-20 10:31:59

标签: php .htaccess laravel-5

我想在切换到Laravel后支持旧网址。

由于路径中不允许使用点.,因此我尝试将user.php?id=3重定向到/user/3文件中的public/.htaccess

我试过

  RewriteCond %{QUERY_STRING} id=(.*)
  RewriteRule ^user\.php$ /user/$1 

但它不起作用。 RewriteRule被忽略。有什么建议被忽略吗?

这是我的完整Laravel .htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    RewriteCond %{QUERY_STRING} id=(.*)
    RewriteRule ^user\.php$ /user/$1

    # Now, rewrite any request to the wrong domain to use www.
    # [NC] is a case-insensitive match
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

RewriteCond %{QUERY_STRING} ^id=(\d+)$
RewriteRule ^user.php$ /user/%1 [L]

答案 1 :(得分:0)

对于RewriteCond的反向引用,您需要使用%1代替$1

检查此代码:

RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^user\.php$ /user/%1 [L]
  

RewriteCond反向引用 :这些是%N形式的反向引用(0 <= N <= 9)。 %1到%9提供对分组部分的访问(同样,在   括号中的模式,来自最后一个匹配的RewriteCond   当前的条件。 %0提供对整个字符串的访问   与该模式相匹配。

参考:http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond