我想要网址:
http://domain.com/category/subcategory/title
指向:
http://domain.com/posts/category/subcategory/title.php
我的 .htaccess 文件如下所示:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^.+
#RewriteCond post/%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ post/$1.php [L]
但是当我取消注释上面的行时它不起作用。
我试过了:
RewriteCond /post/%{REQUEST_URI}.php -f
RewriteCond post/%{REQUEST_URI}.php -f
RewriteCond post%{REQUEST_URI}.php -f
这些都不起作用。我做错了什么?
还有调试这些规则的简单方法吗?比如控制台日志变量之类的?
最佳答案
-f
检查需要完整的文件系统路径。您可以使用 %{DOCUMENT_ROOT}
变量来做到这一点:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{DOCUMENT_ROOT}/post/$1.php -f
RewriteRule ^(.+)$ post/$1.php [L]
关于apache - 将 RewriteCond 与 %{REQUEST_URI} 一起用于 mod_rewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38998802/