2014-08-29 71 views
1

我想重写此网址www.example.com/index.php?page=search & q = string1 & type = string2 to this www.example.com/search/?q = string1 & type = string2 或www.example.com/search?q=string1 & type = string2。htaccess URL重写不适用于所有情况下

我使用的代码,但不工作:

RewriteEngine on 
RewriteRule ^search?([^/]*)$ index.php?page=search&$1 [L,NC] 

有谁能够给予解决?

回答

1

可以在/myprojects/www.mysite.com/.htaccess使用这样的规则:

RewriteEngine on 
RewriteBase /myprojects/www.mysite.com/ 

# external redirect from actual URL to pretty one 
RewriteCond %{THE_REQUEST} /index\.php\?page=(search)\s [NC] 
RewriteRule^%1/? [R=302,L,NE] 

# internal forward from pretty URL to actual one 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^search/?$ index.php?page=search [QSA,L,NC] 

QSA(查询字符串附加)标志保留现有的查询参数,同时增加一个新的。

+0

我知道了。非常感谢。 – 2014-08-29 08:52:29

+0

你的答案有一个小的更正。它应该是 RewriteRule^search /?$ index.php?page = search [QSA,NC,L] – 2014-08-29 11:44:01

+0

好的,现在已经被修正了。 – anubhava 2014-08-29 13:56:48

0

Escape the/in^search?([^ /] *)$。所以,你的代码应该是:

RewriteEngine on 
    RewriteRule ^search?([^\/]*)$ index.php?page=search&$1 [L,NC] 

我用正则表达式测试仪,有时他们帮助:) 继承人链接http://regex101.com/

0

我想你的意思是redirect

www.example.com/index.php?page=search&q=string1&type=string2

www.example.com/search?q=string1&type=string2

这将适用于这种情况下

RewriteEngine on 

# Stop any redirect loop 
RewriteCond %{ENV:REDIRECT_STATUS} 200 
RewriteRule^- [L] 

# if it does not starts with /search 
RewriteCond %{REQUEST_URI} !^/search 

# if the query string start with 
# page=search, then get the rest 
# of the query string 
RewriteCond %{QUERY_STRING} ^page=search&(.*) 

# redirect from: 
# ?page=search&q=string1&type=string2 
# 
# to 
# search?q=string1&type=string2 
RewriteRule ^(.*)$ /search?%1 [R=302,L] # change to 301 

# if the uri starts with search then 
# process the url and query string previously redirected 
# the flag: QSA 
# appends the query string 
RewriteRule ^search/?$ index.php?page=search [QSA,NC,L] 

,并更改R=302R=301

编辑以匹配新requeriments

RewriteEngine on 
RewriteBase /your/folder/here 

RewriteCond %{ENV:REDIRECT_STATUS} 200 
RewriteRule^- [L] 

RewriteCond %{REQUEST_URI} !^/search 
RewriteCond %{QUERY_STRING} ^page=search[&(.*)]? 
RewriteRule ^(.*)$ /search?%1 [R=302,L] 

RewriteRule ^search/?$ index.php?page=search [QSA,NC,L] 
+0

我明白了。非常感谢。 – 2014-08-29 08:51:17