【问题标题】:Laravel: Redirect url of type `user.php?id=3`Laravel:`user.php?id=3` 类型的重定向 url
【发布时间】:2018-07-30 16:35:39
【问题描述】:

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

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

我试过了

  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>

【问题讨论】:

  • "由于不允许在路由中使用点 ." 路由中允许使用点,但路由变量中不允许使用点。不需要所有的重定向
  • @kerbholz 我希望你是对的!如果我尝试Route::get('/user.php',function(){ dd('test'); }) 然后/user.php 返回No input file specified.
  • 嗯,奇怪。 Route::get('/user.py', function () {dd('test');}); 在这里就像一个魅力,使用 Laravel Framework 5.5.28 并且没有 Apache,所以 NO RewriteRules。
  • @kerbholz 好的,我发现了问题 - 它可以在我的 apache 制作服务器上运行,但它不能在带有 Nginx laracasts.com/discuss/channels/general-discussion/… 的 Homestead 上运行......谢谢你的评论.. 我只需要我的生产服务器工作。
  • 很好,很高兴它有效

标签: php .htaccess laravel-5


【解决方案1】:

尝试以下方法:

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

【讨论】:

  • 我尝试过,即使使用^user\.php$,但它不起作用。
  • 您需要通过解释它的作用来改进答案。
【解决方案2】:

对于RewriteCond的反向引用,您需要使用%1而不是$1

检查此代码:

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

RewriteCond 反向引用:这些是 %N (0

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

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2020-04-24
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    相关资源
    最近更新 更多