【问题标题】:htaccess rewrite url error page and idhtaccess 重写 url 错误页面和 id
【发布时间】:2023-04-02 21:35:01
【问题描述】:

我要实现以下目标

  1. mydomain.com/anyword 这样的任何链接都应该重写为 index.php?pg=$1

示例:

mydomain.com/new -->index.php?pg=new

mydomain.com/purchased -->index.php?pg=purchased

mydomain.com/login -->index.php?pg=login
  1. mydomain.com/anyword/anynumber 等任何链接都应重写为 index.php?pg=$1&id=$2

示例:

mydomain.com/new/12 -->index.php?pg=new&id=12

mydomain.com/purchased/240 -->index.php?pg=purchased&id=240

mydomain.com/login/10 -->index.php?pg=login&id=10

我使用了以下 htaccess 代码。 第一个工作正常。 关于第二个,整个参数传给pg,根本没有参数id。

这是在 url 的情况下 mydomain.com/purchased/240 在我的 php $_Get 中只有 'pg' 参数和 'id' 参数 和 pg=/purchased/240

请提出正确的方法。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
RewriteRule ^([^/]+)/(\d+)/.*$ /index.php?pg=$1&id=$2 [NC,QSA]

谢谢

编辑:正确的解决方案,以防有人需要它

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+) /index.php?pg=$1&id=$2 [NC,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]

【问题讨论】:

  • 你遇到了什么错误?
  • 这个问题好像和HTA没什么关系?
  • @Teemu - 为什么不呢?重写规则有一些错误。我也尝试了其他规则。它不工作。
  • 好的。自己找到的。必须为第二条规则再次重复重写条件。第二条规则应该是第一位的。编辑了答案。感谢@starkeen 提到应该提出第二条规则。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

这是因为包罗万象的模式。 (.*) 匹配所有内容并将其重写为目标。对于您的示例,您的第一条规则匹配两个请求,/foobar//foobar/123 被重写为 /index.php?pg=request 。要解决这个问题,您需要修复规则的顺序,您需要将您的特定规则放在包罗万象的规则之前:

RewriteRule ^([^/]+)/(\d+)/?$ /index.php?pg=$1&id=$2 [L]
RewriteRule ((?!index.php).+) /index.php?pg=$1 [L]

【讨论】:

  • 在这种情况下,我收到以下错误:内部服务器错误服务器遇到内部错误或配置错误,无法完成您的请求。请联系服务器管理员 webmaster@xl2tally.casangarnco.com 并告知他们错误发生的时间,以及您所做的任何可能导致错误的事情。服务器错误日志中可能提供有关此错误的更多信息。此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。
  • 好的,我认为错误是由于相同的模式和目标。我已经排除了规则模式中的目标路径。试试更新后的规则。
  • 对不起。得到相同的内部错误。但是,如果我删除特定规则,则第一种类型的 url - 一般重写有效。我认为具体的规则有一些错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多