我想将此网址www.example.com/index.php?page=search&q=string1&type=string2重写为此www.example.com/search/?q=string1&type=string2 或www.example.com/search?q=string1&type=string2。
我使用了代码但没有用:
RewriteEngine on
RewriteRule ^search?([^/]*)$ index.php?page=search&$1 [L,NC]
任何人都可以提供解决方案吗?
答案 0 :(得分: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
(查询字符串追加)标志在添加新查询参数时保留现有查询参数。
答案 1 :(得分:0)
逃离/在^搜索?([^ /] *)$。所以你的代码应该是:
RewriteEngine on
RewriteRule ^search?([^\/]*)$ index.php?page=search&$1 [L,NC]
我使用正则表达式测试人员,有时他们会帮助:) 这是一个链接http://regex101.com/
答案 2 :(得分: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=302
更改为R=301
已编辑以匹配新的广告
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]