我一直在努力让友好的 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>
.ht 访问:
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/
这可能会起作用:
您的正则表达式以
^blog...
. 这将永远不会匹配您发送的内容,因为缺少初始 /。您所说的正则表达式匹配以 开头的 URLblog/...
,但实际上主机名:端口后总是有一个斜杠。请参阅RewriteRule的文档:特别是:
我的例子更通用;如果您需要它用于非常特定的路径,就像您所拥有的那样,那么请务必将其改回并替换
(.*)
分组以及$1
您拥有的内容。