.htaccess从GET变量重定向到url字符串

时间:2023-02-05 20:31:03

I need to redirect

我需要重定向

/search?keywords=somesearchterm

to

/search/somesearchterm

This seems incredibly basic but I've been pounding my head against it for an hour.

这似乎非常基本,但我一直在冲击它一小时。

Thanks for taking the time to look at this.

感谢您抽出宝贵时间来看看这个。

3 个解决方案

#1


8  

You want to implement what is called a "301 Redirect" with mod_rewrite.

您想使用mod_rewrite实现所谓的“301重定向”。

RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm

adding regular expressions:

添加正则表达式:

RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]

R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.

R = 301表示提供301标题重定向,因此用户的URL在浏览器中更改,L表示如果匹配则不再处理任何重写规则。


If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:

如果你想反过来 - 换句话说,如果有人去了mysite.com/search/asearchterm并且你希望URL保持不变,但是“幕后”你希望它加载某个服务器脚本,做这个:

RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]

#2


0  

Take a look at my answer for another similar thread!

看看我对另一个类似线程的回答!

Simulate file structure with PHP

使用PHP模拟文件结构

This should be a decent answer for you!

这应该是一个体面的答案!

#3


0  

You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :

您无法在RewriteRule指令中匹配aginst查询字符串。使用%{THE_REQUEST}或%{QUERY_STRING}服务器变量来匹配查询字符串:

The following rule works fine for this redirection

以下规则适用于此重定向

 RewriteEngine On
 RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
 RewriteRule ^ /search/%1? [NE,NC,R,L]

RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

#1


8  

You want to implement what is called a "301 Redirect" with mod_rewrite.

您想使用mod_rewrite实现所谓的“301重定向”。

RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm

adding regular expressions:

添加正则表达式:

RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]

R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.

R = 301表示提供301标题重定向,因此用户的URL在浏览器中更改,L表示如果匹配则不再处理任何重写规则。


If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:

如果你想反过来 - 换句话说,如果有人去了mysite.com/search/asearchterm并且你希望URL保持不变,但是“幕后”你希望它加载某个服务器脚本,做这个:

RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]

#2


0  

Take a look at my answer for another similar thread!

看看我对另一个类似线程的回答!

Simulate file structure with PHP

使用PHP模拟文件结构

This should be a decent answer for you!

这应该是一个体面的答案!

#3


0  

You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :

您无法在RewriteRule指令中匹配aginst查询字符串。使用%{THE_REQUEST}或%{QUERY_STRING}服务器变量来匹配查询字符串:

The following rule works fine for this redirection

以下规则适用于此重定向

 RewriteEngine On
 RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
 RewriteRule ^ /search/%1? [NE,NC,R,L]

RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]