【发布时间】:2021-05-14 21:48:23
【问题描述】:
假设我要转换:
http://localhost/profile/view?q=James
进入
http://localhost/profile/view/James
问题是我必须为此创建一个名为“view”的新文件夹。有没有办法让我不需要为此创建一个文件夹?这个的 htaccess 规则是什么?
【问题讨论】:
假设我要转换:
http://localhost/profile/view?q=James
进入
http://localhost/profile/view/James
问题是我必须为此创建一个名为“view”的新文件夹。有没有办法让我不需要为此创建一个文件夹?这个的 htaccess 规则是什么?
【问题讨论】:
不,你没有。您在.htaccess 中使用RewriteRule。这有效地将正则表达式与您的 URL 匹配,然后按照您的说明对其进行转换(很像:preg_replace)。
RewriteRule ^profile/view/([\w]+)/?$ path/to/profile/file.php?q=$1 [L]
然后用户导航到:
http://www.website.com/profile/view/john
服务器将用户指向:
http://www.website.com/path/to/profile/file.php?q=john
【讨论】:
RewriteRule ^http://localhost/profile/posts/([\w]+)/?$ http://localhost/profile/posts?name=$1 [L]?
RewriteCond %{HTTP_HOST} ... 获取 URL,您可以检查 http or https 是否具有类似的条件,然后使用重写。但在不知道输入和预期输出的情况下,很难准确地说出
RewriteRule ^profile/(\w+)/posts$ profile/posts.php?name=$1