[英]URL rewriting for sub-directory without changing base
我要重写以下URL:
https://www.example.com/tools/tool.php?id=predict-backlinks
至:
https://www.example.com/tools/predict-backlink
如此处建议: 子文件夹的URL重写
现在,工作网址为https://www.example.com/tools/tool/predict-backlink
以下代码可以正常工作,但已在URL中添加了tool
。 我希望它能在没有URL的tool
的情况下工作。
Options -MultiViews
RewriteEngine On
RewriteBase /tools/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tool/(.*) tool.php?id=$1 [L]
此规则应为您做到:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(.+) [NC]
RewriteRule ^(.*)$ https://www.example.com/tools/%1? [R=301,NC,L]
这是抓取您的查询id=
并使用RewriteRule将其使用%1
附加到URL的末尾。
?
是否可以阻止原始查询也不会出现在末尾。 整个重写使用301重定向完成。
在测试之前,请确保清除缓存。
编辑:
感谢您更新问题,现在更加清晰。 之所以添加tool/
是因为您的RewriteRule前缀^tool/(.*)
这部分。 您要自己添加它。
只需从RewriteRule中删除tool/
,它无需添加tool
就可以工作。
RewriteRule ^(.*) tool.php?id=$1 [L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.