繁体   English   中英

使用 php 代码获取 url 路径,无需 htaccess

[英]Obtain url paths using php code without htaccess

有没有办法在没有 htaccess 的情况下使用 php 代码获取 url 路径(例如index.php/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]

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM