Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php .htaccess mod rewrite将查询字符串重定向到路径_Php_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Php .htaccess mod rewrite将查询字符串重定向到路径

Php .htaccess mod rewrite将查询字符串重定向到路径,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我最近更新了我的网站以使用: www.website.com/path/123456 而不是: www.website.com/path/file.php?id=123456 为了不破坏旧的链接/书签,我想将旧的查询字符串重定向到新的路径 我试过: RewriteRule ^path/file.php?id=?$ /path/$1 [L,R] 和一些变化,但没有运气。我需要如何设置.htaccess来执行此重定向?问题似乎是您的正则表达式没有捕获参数,如下所示: RewriteRule ^

我最近更新了我的网站以使用:

www.website.com/path/123456
而不是:

www.website.com/path/file.php?id=123456
为了不破坏旧的链接/书签,我想将旧的查询字符串重定向到新的路径

我试过:

RewriteRule ^path/file.php?id=?$ /path/$1 [L,R]

和一些变化,但没有运气。我需要如何设置.htaccess来执行此重定向?

问题似乎是您的正则表达式没有捕获参数,如下所示:

RewriteRule ^path/file.php?id=(.*)$ /path/$1 [L,R]

在正则表达式中,使用圆括号捕获稍后要引用的字符串部分,如$1。.*将匹配任何内容。例如,如果您只想捕获数字,可以将其更改为\d+。

您可以在文档\u ROOT/.htaccess文件中使用此代码:


这对我不起作用,什么都没发生。我只是像平常一样访问了链接。htaccess位于/path/上方的根目录下。还有其他规则,但我已尝试重新排列和删除其他规则以使其工作。/path/没有自己的。htaccess,在应用程序的根目录下只有一个。然而,它有更多的规则。当删除所有规则并用您给我的文件替换文件时,我得到一个404页面,其中包含请求的URL/path/file.php,但在该服务器上找不到。好的,相同的规则在我的apache中可以正常工作,URL完全相同。通过将相同的垃圾随机文本放在.htaccess上,验证是否启用了.htaccess,并查看当您在浏览器中访问页面时,它是否会生成500个内部服务器错误?我确实得到了500个,其他规则在文件中时也会起作用。这对我不起作用,什么都没有发生。我只是像平常一样访问了链接。
RewriteCond %{REQUEST_URI} ^/path/$ [NC]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule .* /path/$1 [R]
RewriteCond %{REQUEST_URI} ^/path/file.php$ [NC]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule .* /path/$1 [R]
RewriteRule ^path/file.php?id=(.*)$ /path/$1 [L,R]
RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /path/file\.php\?id=(\d+) [NC]
RewriteRule ^ /path/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^path/(\d+)/?$ path/file.php?id=$1 [L,QSA,NC]