简体   繁体   English

Apache mod_rewrite path to query string for specific path format

[英]Apache mod_rewrite path to query string for specific path format

I've been working on this for weeks.我已经为此工作了好几个星期。 I want to be able to use a friendly URL to pass variables to a PHP script.我希望能够使用友好的 URL 将变量传递给 PHP 脚本。 For example, if someone uses this URL:例如,如果有人使用这个 URL:

https://example.com/foo/bar/who https://example.com/foo/bar/who

I would like my PHP script to receive this:我希望我的 PHP 脚本能收到这个:

https://example.com/index.php/?var1=foo&var2=bar&var3=who https://example.com/index.php/?var1=foo&var2=bar&var3=who

The catch is that I ONLY want to do this rewrite when there are three vars in the path.问题是我只想在路径中有三个变量时进行重写。 If there are fewer or more than three, I do not want to rewrite the URL.如果少于或多于三个,我不想重写 URL。

I've seen several other explanations related to this type of rewrite but nothing quite like this.我已经看到与这种类型的重写相关的其他几种解释,但没有像这样的。

This is almost working, but not quite.这几乎可以工作,但并不完全。 I only want the rewrite done if there's something present for those first three variables.如果前三个变量存在某些东西,我只希望完成重写。 I know this is incomplete, but with this method at least the REQUEST_URI contains the values I can parse.我知道这是不完整的,但使用这种方法,至少 REQUEST_URI 包含我可以解析的值。 But again, I only want to do this when there are three vars in the path.但同样,我只想在路径中有三个变量时这样做。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^index\.php/([^/]*)/([^/]*)/([^/]*)$ index.php [L]

Thank you.谢谢你。

You were pretty close, have it this way.你已经很接近了,就这样吧。 Please make sure you clear your browser cache before testing your URLs.请确保在测试 URL 之前清除浏览器缓存。 You had already created back references only thing you needed to use them, which I have added those now.您已经创建了仅需要使用它们的反向引用,我现在已经添加了这些。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?var1=$1&var2=$2&var3=$3 [L]

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

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