问题
Question regarding redirection using htaccess. On a GET search form submit I get the following url:
http://www.example.com/search/?criteria=foo&filter=all&date=all&submit=search&page=1
The above works fine. Then I have this rewrite rule in my htaccess:
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/$ search.php?criteria=$2&filter=$3&date=$4&submit=$1&page=$5 [L]
The above also works fine. It allows me to use the following structure for my search results:
http://www.example.com/search/foo/title/all/1/
Now my question is when I click submit I would like it to use the new clearner url structure however it uses the messier one with the question marks and the equal signs. Now automatically I was thinking maybe I have to do a redirect in PHP however if I can do it with htaccess I would be happier as it means cleaner code.
I also understand I can do this with JavaScript by intercepting the click and creating the seo friendly url but again if their is a way with htaccess I would prefer that.
Hope you understand what im trying to achieve and many thanks for reading,
fl3x7
p.s im pretty new to htaccess so if you can explain/ guide etc that would be great
回答1:
You cannot do it solely with .htaccess.
You can do it with javascript (intercept the onclick event, construct the url string using the user entered data then redirect the page to this url), but this is not really reliable considering that javascript runs on the client side.
My suggestion is to use PHP for this. The process is the same, but I'll use POST instead of GET as a method for the form. Before the page gets rendered, detect the $_POST
data and construct the $url
using it. Then redirect the page to that $url
, using header("Location: ".$url);
It will be best to use exit();
after this, to make sure that the code below it will not get executed as the redirect is made.
回答2:
You cannot do this without Javascript.
An HTML form using the GET method will send form values in the query string.
There is no way to tell it to put the values in the path instead.
回答3:
You probably should do this with Javascript, however you can do it in htaccess by using redirects. I think it would be something like:
RewriteRule ^search\.php?criteria=(.*)&date=(.*)&filter=(.*)&page=(.*)&submit=([^&])* /search/$1/$3/$2/$5/$4 [R=302, L]
I hope this will work because query-strings are supposed to have their values arrive in alphabetical order.
However, a word of warning: This might not work so please take it as just a suggestion for something to try.
回答4:
There is absolutely nothing pretty in using seo urls for search.
It is unlikely that google guys are less smart than you, but they're using STANDARD query string way for the search. Because every other way of doing search is ugly
来源:https://stackoverflow.com/questions/7365999/rewrite-url-when-a-form-is-submitted