Forum Moderators: phranque

Message Too Old, No Replies

Challenging Mod rewrite() Issue

I have made a good start, but its a tricky one!

         

jamess

11:43 pm on Feb 8, 2008 (gmt 0)

10+ Year Member



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.

g1smd

12:14 am on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Your initial test fails as "/" is not listed in [A-Z0-9] and therefore doesn't match.

You need to capture more back references, and redirect accordingly.

Your question also isn't 100% clear as you talk about redirects but some of your code shows rewrites instead.

jamess

12:21 am on Feb 9, 2008 (gmt 0)

10+ Year Member



/ is not a search parameter, its only used as the marker between search and the tag itself.

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.

jamess

12:23 am on Feb 9, 2008 (gmt 0)

10+ Year Member



Wait I also follow what you mean; You need to access domain.com/search/ for the page to show and domain.com/search fails (I will fix this and post amended code)

g1smd

12:25 am on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK. I had thought you were looking for /search/tag1/tag2 for some reason.

I am right in the middle of editing some PHP code, and should have read your question with a clearer head.

jamess

12:28 am on Feb 9, 2008 (gmt 0)

10+ Year Member



There - fixed, removed the preceding / - hope that clarifies.

RewriteRule ^search/([0-9a-zA-Z!]+)$ search.php?tag=$1
RewriteRule ^search$ search.php?

However the original problem remains and I dont have the current skills or knowledge to come up with a complex enough regexp to work :(

g1smd

12:29 am on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** "windows - vista" and go to search/windows-vista ***

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.

g1smd

12:30 am on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Which of your codes is supposed to be a redirect and which a rewrite?

jamess

12:34 am on Feb 9, 2008 (gmt 0)

10+ Year Member



a)

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.

g1smd

12:38 am on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[A-Z0-9] allows for letters and numbers, but doesn't allow for + or - literal characters.

jamess

12:46 am on Feb 9, 2008 (gmt 0)

10+ Year Member



I also need it to allow single spaces and the & (unencoded) and to have multiple tags (as in several of those sets of validations)

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 :)

jamess

2:08 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



anyone able to help?

jdMorgan

7:40 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We are here to help you learn to write your own code, so please do not ask other members to write your code for you. If you need to hire contract help, you can try contacting likely candidate members by stickymail.

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

jamess

5:29 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Well I was hoping for some insightful help.

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

wilderness

5:38 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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]

jdMorgan

6:50 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to accept pretty much any character, how about a pattern that accepts anything except a slash or a blank search term?

RewriteEngine on
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^search/$ search.php? [R=301,L]
RewriteRule ^search/([^/]+)$ /search.php?tag=$1 [L]

Jim

jamess

11:31 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Thanks JD, a few parts there for me to look into

[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.

g1smd

12:01 am on Feb 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If there is no [R] then you have a rewrite, not a redirect.

A rewrite allows you to connect a URL (that the user sees) to a filepath within the server, without exposing what that filepath actually is.