.htaccessModRewrite:未找到网址
我的目标是使用 .htaccess重定向http://ipaddress/directory/file.php?id=47到http://ipaddress/directory/47。的ID参数会有所不同,可以象1,2,3,10,编号中的任何串20,300等我当前htaccess的文件是如下:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /directory/%1? [R=301,L]
重写确实将 url 更改为我想要的!但是我在重定向后收到错误消息,指出未找到 - 在此服务器上找不到请求的 URL。我不确定为什么重定向有效,但没有加载具有id参数和实际 PHP 文件的上一页。
谢谢!
回答
使用您展示的样品,请尝试以下规则。确保您的 htaccess 存在于根文件夹中。确保在测试 URL 之前清除浏览器缓存。
##Making RewriteEngine ON here.
RewriteEngine on
##Checking condition if THE_REQUEST is in same format which OP has mentioned for URI and mentioning Rule then.
RewriteCond %{THE_REQUEST} s/file.php?id=(d+)s [NC]
RewriteRule ^ /directory/%1? [R=301,L]
RewriteRule ^directory/(d+)/?$ /directory/file.php?id=$1 [NC,L,QSA]