Forum Moderators: phranque
hello everyone, thanks for reading.
Background:
making a web application, most of the pages have fully functional mod_rewrites but I need one for a search box where you can search by tag.
Current Code: (Everything removed apart from whats relevant)
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^search/([0-9a-zA-Z!]+)$ search.php?tag=$1
RewriteRule ^search/$ search.php?
What happens just now is; if you go to domain.com/search it shows you search.php - PERFECT!
NOTE: FORM VALUES PASSED BY $_GET
Now on the search page, if you search for a single word like "tag" without the quotes it redirects you to domain.com/search/tag - Again thats perfect.
However if you do a search for say "tag tag2" then the mod_rewrite fails and I am taken to this page:
domain.com/search.php?tag=tag+tag2&search=Search
(please note that search is the name of the submit button and Search is its value - thats where they come from)
What I would like to happen is that the user is taken to
search/tag1+tag2
I would also like users to be able to enter LETTERS (any case), NUMBERS, Spaces - and +, & in the search box and as many tag words as they like, so they could search for "windows vista" and go to search/windows+vista or "windows+vista" and go to search/windows+vista or search for "windows - vista" and go to search/windows-vista
and so on.
In effect whatever they type in the search box takes them to search/THE ENTRY but without spaces and not allowing special characters like quotes etc
Does that make sense? What I dont want is them to be taken to search.php as that ruins the whole effect! I can handle all the decoding of the search query and stuff, I only need help with the mod_rewrite rule.
so domain.com/search takes you to search.php without a query being ran, but domain.com/search/tag would take you to search.php and would search the DB for tag. That part is working, what isnt working is allowing multiple tags to be entered in a text box and for the multiple tags to be shown as domain.com/search/tag1+tag1+tag2
They are rewrites, but I use PHP to do redirects to ensure that the .php extensions arent seen on pages with forms which can be submitted.
Hope that helps.
Hang on one more moment. Clarity of problem definition is very important here.
Google uses the + to separate words out, and the - to negate a search query.
They would do this example search element as "windows+-vista" in the URL. That is, it would have both the + and the - in it.
I guess then tag+-tag2 would need to be allowed too, but I can handle the PHP logic to work out what to do when passed such a string to search the DB for.
b) PHP does redirects, after a page is submitted (like a new user with user id of 1 it redirects to domain.com/user/view/1) and the htaccess is used to make sure that the URL works correctly using mod_rewrite();
Apache/htaaccess is for rewrites and php for redirects or the links (for example my href to home is ahref="/home" etc.
could you write that out for me? the mod_rewrites i have I just cobbled together from a bajillion different sources and then amended, but new things I'll need help with.
Please :)
Otherwise, please see the regular expressions tutorial cited in our forum charter [webmasterworld.com]; Your specific questions with regard to a particular concept that you're demonstrably trying to learn will be welcome.
From the old proverb: If we feed you a fish, you'll come back hungry tomorrow. If we teach you to fish, you can feed yourself -- and teach others to fish as well.
Thanks,
Jim
Perhaps suggesting what concept I should learn or something, its not like I havent tried out a load of combinations before I posted - its just I dont understand what approach to take or even if its possible. So whilst suggesting I add a + or - is a little helpful, I really wanted someone to read my post.
RewriteRule ^search/([0-9a-zA-Z!+-&]+)$ search.php?tag=$1
Yeah but nobody has suggested a decent tutorial
Select library [webmasterworld.com] from the light blue bar near top of page:
Mod_Rewrite & Regular Expressions [webmasterworld.com]
[R] stands for Redirect. The default is 302-Temporarily Moved. This can be set to any number between 300 and 400, by entering it as [R=301] or [R=YourNumberHere], but 301 (Permanently Moved) and 302 (Temporarily Moved) are the most common.
(If you just use [R] this will work, and defaults to 302-Temporarily Moved)
** Do not use this flag if you are trying to make a 'silent' redirect.
If I want a redirect thats always going to happen, but I don't want the user to ever see .php etc in the browser do I need to use one of those flags? At the moment I dont use any flags, but i will add in some [l] ones.
I will try out a few things and get back to you.