我一直在努力让友好的 URL 发挥作用。
在我的网站上有类似这样的内容:
http://www.example.com/blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant
我希望它看起来像这样:
http://www.example.com/blog/Reasons+to+Use+a+Small+Business+Consultant
我尝试编辑 httpd.conf 和 .htaccess,但没有得到任何回应。
httpd.conf:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/sites/example
ServerName www.example.com
<Directory /var/www/sites/example/blog>
Options FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
.htaccess 文件:
RewriteEngine On
RewriteRule ^blog/Reasons+to+Use+a+Small+Business+Consultant$ blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant
日志级别警报重写:trace3
跟踪日志表明它应用了该模式但我仍然看到:
http://www.example.com/blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant
[Thu Oct 25 18:39:17.821619 2018] [rewrite:trace3] [pid 27933] mod_rewrite.c(470): [client 123.321.64.56:58790] 123.321.64.56 - - [www.example.com/sid#558610b6a4a8][rid#558610d3d740/initial] [perdir /var/www/sites/example/blog/] strip per-dir prefix: /var/www/sites/example/blog/newpost.php -> newpost.php, referer: http://www.example.com/blog/
[Thu Oct 25 18:39:17.821648 2018] [rewrite:trace3] [pid 27933] mod_rewrite.c(470): [client 123.321.64.56:58790] 123.321.64.56 - - [www.example.com/sid#558610b6a4a8][rid#558610d3d740/initial] [perdir /var/www/sites/example/blog/] applying pattern '^blog/Reasons+to+Use+a+Small+Business+Consultant$' to uri 'newpost.php', referer: http://www.example.com/blog/
[Thu Oct 25 18:39:17.821654 2018] [rewrite:trace1] [pid 27933] mod_rewrite.c(470): [client 123.321.64.56:58790] 123.321.64.56 - - [www.example.com/sid#558610b6a4a8][rid#558610d3d740/initial] [perdir /var/www/sites/example/blog/] pass through /var/www/sites/example/blog/newpost.php, referer: http://www.example.com/blog/
答案1
这可能会有用:
RewriteEngine On
RewriteRule /blog/(.*) %{HTTP_HOST}/blog/newpost.php?$1 [L]
您的正则表达式以 开头^blog...
。由于缺少开头的 /,因此它永远不会与您发送的内容匹配。您的正则表达式表示匹配以 开头的 URL blog/...
,但实际上 hostname:port 后面总是有一个斜杠。
请参阅文档重写规则: 具体来说:
该模式最初将与 URL 中主机名和端口之后、查询字符串之前的部分进行匹配(例如“/app1/index.html”)。
我的例子更加通用;如果您需要它用于一条非常具体的路径,就像您已经有的一样,那么一定要将它改回来并用您已有的内容替换(.*)
分组。$1