应用 Htacces 规则后,在 PHP 项目中访问 PHP 和非 PHP 的 URL
RewriteRule ^c/([a-zA-Z0-9-/]+)$ category.php?id=$1 [L]
RewriteRule ^p/([a-zA-Z0-9-/]+)$ detail.php?post=$1 [L]
在这里,我访问了两个网址,例如 www.example.com/c/category-name
和 www.example.com/category.php?id=12
但我只需要 www.example.com/c/category-name
网址。我不想在这个页面上出现重复的网址。
最佳答案
对于您显示的示例,请尝试遵循 htaccess 规则。确保在测试您的网址之前清除浏览器缓存。
RewriteEngine ON
##Internal rewrite rules.
RewriteCond %{HTTP_HOST} ^(?:www\.)?example.com$ [NC]
RewriteRule ^c/([\w-]+)/?$ category.php?id=$1 [QSA,NC,L]
##External redirect rules.
RewriteCond %{HTTP_HOST} ^(?:www\.)?example.com$ [NC]
RewriteCond %{THE_REQUEST} \s/category\.php?id=(\S+)\s [NC]
RewriteRule ^ /c/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*/(.*)/?$ category.php?id=$1 [QSA,NC,L]
关于apache - Htaccess 中 RewriteRule 后的 PHP 和非 PHP 访问 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72089351/