我已经四处寻找并尝试了自己,无法解决这个问题。
这是应用重写之前的URL:
http://example.com/job-search/?searchText=GOOGLE+INC.&searchCity=Enter+US+City+or+Zipcode&searchYear=14&action=search&searchJobTitle=Enter+Job+Title+%2F+Role+Name
这是应用了以下重写规则后的网址:
http://example.com/job-search/GOOGLE-INC./Enter-US-City-or-Zipcode/14/search/Enter-Job-Title
我在.htaccess中有以下规则。当我拥有上述所有字段的URL时,它运作良好。
RewriteEngine On
RewriteBase /job-search/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]
重写失败,当它变为空值时。规则不起作用,我得到以下网址。
http://example.com/job-search/?searchText=GOOGLE-INC.&searchCity=&searchYear=14&action=search&searchJobTitle=
在三个参数(searchText或searchCity或SearchJobTitle)中,一次只有一个参数具有值,而其他两个参数将具有空值。另外,只是想确保当用户键入像http://example.com/job-search/这样的主页URL时,它不应该失败(我尝试了一些并且失败了)。当它们是空的时候也想避免双斜线。
以下是我希望看到的输出:
例如,当searchCity和SearchJobTitle为空时,输出URL应该为
http://example.com/job-search/GOOGLE-INC./14/search
当searchText和searchCity为空且searchJobTitle值为' Applicaton-Engineer'时,输出网址应如下所示
http://example.com/job-search/14/search/Application-Engineer
你能帮我解决正确的规则吗?感谢您的帮助 !
答案 0 :(得分:1)
将规则更改为:
RewriteEngine On
RewriteBase /job-search/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=\s [NC]
RewriteRule ^ %1/%2/%3/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]
RewriteRule ^([^/]+)/(\d+)/([^/]+)/?$ ?searchText=$1&searchYear=$2&action=$3 [NC,L,QSA]