Apache 如何在htaccess中避开301重定向中的单个问号

Apache 如何在htaccess中避开301重定向中的单个问号,apache,.htaccess,redirect,mod-rewrite,Apache,.htaccess,Redirect,Mod Rewrite,基本上,我想做一个301重新直接从一个特定的动态页面,有一个问号字符(?)的URL到一个静态页面 我想这样做的原因是,这个网页是有效的搜索引擎优化,但它将受益于自己的网页现在。我已经多次使用重写规则重定向查询字符串,但我似乎不知道如何转义,比如说,一个具有?的特定url?。例如: Redirect 301 /page.php?page_id=1 /new-page/ Redirect 301 /page.php?page_id=10 /cool-product/ 我不需要执行以下操作: Rew

基本上,我想做一个301重新直接从一个特定的动态页面,有一个问号字符(?)的URL到一个静态页面

我想这样做的原因是,这个网页是有效的搜索引擎优化,但它将受益于自己的网页现在。我已经多次使用重写规则重定向查询字符串,但我似乎不知道如何转义,比如说,一个具有?的特定url?。例如:

Redirect 301 /page.php?page_id=1 /new-page/
Redirect 301 /page.php?page_id=10 /cool-product/
我不需要执行以下操作:

RewriteRule ^page/([0-9]+)/([a-zA-Z0-9_-]+)/$ ./page.php?page_id=$1 [R=301,L]

您可以使用
mod_rewrite
创建针对查询字符串运行的规则,如:

RewriteCond %{REQUEST_URI}  ^/page\.php$
RewriteCond %{QUERY_STRING} ^page_id=1$

下面是关于这个主题的一篇不错的文章:

您可以使用
mod\u rewrite
创建针对查询字符串运行的规则,如:

RewriteCond %{REQUEST_URI}  ^/page\.php$
RewriteCond %{QUERY_STRING} ^page_id=1$

以下是关于这个主题的一篇不错的文章:

您不能使用
重定向
指令将带有查询字符串的url重定向到静态页面,您需要使用
mod rewrite
,因此请尝试以下方法:

RewriteEngine on

RewriteCond %{THE_REQUEST} /page\.php\?page_id=1 [NC]
RewriteRule ^ /newpage/? [L,R]
RewriteCond %{THE_REQUEST} /page\.php\?page_id=10 [NC]
RewriteRule ^ /anotherpage/? [L,R]

目标url末尾的空问号很重要,因为它会从新url中丢弃旧的查询字符串。

您不能使用
redirect
指令将带有查询字符串的url重定向到静态页面,您需要使用
mod rewrite
,因此请尝试以下操作:

RewriteEngine on

RewriteCond %{THE_REQUEST} /page\.php\?page_id=1 [NC]
RewriteRule ^ /newpage/? [L,R]
RewriteCond %{THE_REQUEST} /page\.php\?page_id=10 [NC]
RewriteRule ^ /anotherpage/? [L,R]

目标url末尾的空问号很重要,因为它会从新url中丢弃旧的查询字符串。

仅当请求URI为Classified/并重定向到/ads/URI:
http://www.example.com/classifieds/?do=detailpage&cat=3&id=14
http://www.example.com/ads/

# Redirects ONLY if the URI is classifieds/ and Strips the do= Query Strings
# from the destination /ads/ URL|URI
RewriteCond %{QUERY_STRING} ^do=(.*)$ [NC]
RewriteRule ^classifieds/$ http://www.example.com/ads/$1? [R=301,L]

仅当请求URI为classifieds/并重定向到/ads/URI:
http://www.example.com/classifieds/?do=detailpage&cat=3&id=14
http://www.example.com/ads/

# Redirects ONLY if the URI is classifieds/ and Strips the do= Query Strings
# from the destination /ads/ URL|URI
RewriteCond %{QUERY_STRING} ^do=(.*)$ [NC]
RewriteRule ^classifieds/$ http://www.example.com/ads/$1? [R=301,L]