【问题标题】:Obtain url paths using php code without htaccess使用没有htaccess的php代码获取url路径
【发布时间】:2020-07-16 14:56:21
【问题描述】:

有没有办法使用没有htaccess的php代码获取url路径(例如index.php/sales,只提取'sales')? 我有一个 mvc 项目(使用 apache)并且工作正常,但不得不转移到使用 nginx 的 vagrant 服务器。从阅读中,我必须要么将 htaccess 规则转换为 nginx 服务器规则。但是盒子是预先配置了nginx的。不确定这里是否有工作。欢迎提出建议和解决方法。谢谢。

我的 htaccess 文件是:

RewriteBase /
RewriteCond %{REQUEST_FILENAME}%  !-d
RewriteCond %{REQUEST_FILENAME}%  !-f

# Do not route static files in the public directory
RewriteRule \.(js|css|svg|gif|jpg|jpeg|png)$ -                 [L]

RewriteRule ^/?editcategory/(.*?)/?$       /editcategory?categoryid=$1  [L]
  
# Route everything else to index.php in the public directory
# Note: name-router get variable used for public user profiles
RewriteRule ^(.*)$                index.php?name-router=$1 [QSA,L]

P.S:我对 nginx 很陌生,所以不想让事情变得更复杂。

【问题讨论】:

    标签: php .htaccess url-routing


    【解决方案1】:

    如果您对此进行研究,它可能只是您正在寻找的答案: Get the full URL in PHP

    但您最有可能在寻找这个: $_SERVER['REQUEST_URI']

    【讨论】:

      【解决方案2】:

      AFAIK 我不认为你可以不重写就实现你想要的。

      如果你已经从 apache 改成了 nginx,我想这就是你要找的:

      server {
      
        root /var/www/mysite.com;
      
        index index.php;
        server_name mysite.com;
      
        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
      
        location / {
          try_files $uri $uri/ @fallback;
        }
      
        location @fallback {
          rewrite ^/(.*)$ /index.php?section=$1 last;
        }
      
      }
      

      来源:https://serverfault.com/a/851010

      如果不重写网址,您将无法访问 index.php/ 之后的网址中的数据,例如: index.php/sales。要直接在 url 中解析数据属性,您必须以正确的方式定义数据以执行 GET 请求。使用这种格式 "?key1=val1&key2=val2&key3..." 等,因此您依赖于重写。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-08
        • 2017-06-08
        • 1970-01-01
        • 2016-07-10
        • 1970-01-01
        • 2020-10-01
        • 2020-11-23
        相关资源
        最近更新 更多