如何将两个查询字符串转换为一个 URL


htacess how to convert two query strings to one url

  Options +FollowSymlinks
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}'.php -f
  RewriteRule ^news/([^/]+)  /casino2/newsdetail.php?newsid=$1 [NC]

当前使用上述代码将http://localhost/casino2/newsdetail.php?newsid=3转换为 http://localhost/casino2/news/3现在我试图从两个查询字符串中制作一个 url 有什么问题,例如如果 url localhost/casino2/newsdetail.php?newsid=3 并且pagename=article URL 应该localhost/casino2/news/article可以这样做吗?

首先,这些规则似乎与你所说的相反 - 即它们将"/localhost/casino2/news/4711"重写为"/casino2/newsdetail.php?newsid=4711"。

如果这是您的真正意思,您需要做的是将两个项目保存到两个变量中,而不仅仅是一个变量,如下所示:

RewriteRule ^news/([^/]+)/([^/]+)  /casino2/newsdetail.php?newsid=$1&pagename=$2 [NC]

如果 newsid 始终为 1,这样你就不需要把它传递给 php,你只需在替换中创建它,就像这样

RewriteRule ^news/([^/]+)  /casino2/newsdetail.php?newsid=1&pagename=$1 [NC]

这将创建以下重写:

/

news/article ->/casino2/newsdetail.php?newsid=1&pagename=article/news/somethingelse ->/casino2/newsdetail.php?newsid=1&pagename=article

但是,如果您希望能够选择不同的 newsid,则需要将其作为 URL 的一部分或作为查询字符串传递到某个地方。

需要明确的是,规则的应用方式是

RewriteRule "What the URL looks like in the browser" "What my web server expects" 

如果你有问题,我强烈建议你打开很多调试日志记录,这将是一个很大的帮助。相关的配置行是

RewriteLog /path/to/logfile
RewriteLogLevel 3