Forum Moderators: phranque

Message Too Old, No Replies

query being appended to RewriteRule results

         

wavygravy2k

11:26 pm on Oct 25, 2011 (gmt 0)

10+ Year Member



I wrote a rule that does the following:

RewriteRule ^thefolder/(.*) search.php?theid=$1

so http://example.com/cards/search.php?theid=1000 turns into

http://example.com/cards/thefolder/1000

On the page above I have query buttons so if the RewriteRule is implemented and a user runs another query, the query URL is being appended to the RewriteRule. Example:

http://example.com/cards/thefolder/1000?theid=1001

I want to be able to revert back to http://example.com/cards/search.php?theid=1001 when a query button is clicked.

Hope this makes sense. any ideas?

g1smd

11:42 pm on Oct 25, 2011 (gmt 0)

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



I wrote a rule that does the following:

RewriteRule ^thefolder/(.*) search.php?theid=$1

so http://example.com/cards/search.php?theid=1000 turns into

http://example.com/cards/thefolder/1000

It might do that if you add protocol and domain name to the rule target and the [R=301,L] flags, but as written, no your rule does not do what you say at all.

As a rewrite it does the exact opposite of what you say. Be aware that mod_rewrite cannot "make" new URLs. YOU make the new URLs by changing the links on your pages. Only once that link is clicked, can mod_rewrite actually do something. What it does is accept the URL request
http://example.com/cards/thefolder/1000
and then fetch content from inside the server from
/cards/search.php?theid=1000


Mod_rewrite alters the internal file pointer, not the external URL.

Always add the [L] flag to every RewriteRule.

wavygravy2k

5:36 pm on Oct 26, 2011 (gmt 0)

10+ Year Member



Adding [R=301,L] appears to have worked. Thank you!

On my machine at home I had to hit submit twice for the query results to finally appear but it may have been a caching issue. On my other machine at work it functioned as expected.

g1smd

12:20 am on Oct 27, 2011 (gmt 0)

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



Do you really want the user to request one URL then immediately see the URL listed in the browser address bar change to something else?

wavygravy2k

2:26 am on Oct 27, 2011 (gmt 0)

10+ Year Member



I would prefer that the URL stay when the page is visited but if a user clicks the Submit button the URL is appended as I described in the first post.

I tried adding [L] to each rule but that didn't seem to work.

g1smd

6:05 am on Oct 27, 2011 (gmt 0)

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



If you want the URL to stay the same then code the RewriteRule as a rewrite and not as a redirect.

Post your code. Use example.com for any and all URLs.

wavygravy2k

6:03 pm on Oct 27, 2011 (gmt 0)

10+ Year Member



Current code:
RewriteEngine on
RewriteBase /cards
RewriteRule ^image/(.*) first.php?id=$1
RewriteRule ^search/(.*) search.php?book=$1
RewriteRule ^thefolder/(.*) search.php?theid=$1&theid1=By+id [R=301,L]


Original Problem (when [R=301,L] is not used):
If the user goes here: example.com/cards/thefolder/[myidnumber] the URL to stays the same.

When a user clicks the submit button to do a different search the URL is appended the rewritten URL:

example.com/cards/thefolder/[myidnumber]?theid=[myidnumber]&theid1=By+id

Result:
The search results for example.com/cards/thefolder/[myidnumber] are still being used. Bascially, the new search isn't working and the page doesn't really change.

Current Behavior:
If the user goes here: example.com/cards/thefolder/[myidnumber] the URL switches to example.com/cards/search.php?theid=[myidnumber]&theid1=By+id.

When a user clicks the submit button to do a different search the search results come back correctly.

Desired Result:
If the user goes here: example.com/cards/thefolder/[myidnumber] I would like the URL to stay the same.

When a user clicks the submit button to do a different search I would like the URL to change back to example.com/cards/search.php?theid=[myidnumber]&theid1=By+id so the search results come back correctly.

g1smd

6:29 pm on Oct 27, 2011 (gmt 0)

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



Add the [L] flag to every RewriteRule.

It is the browser that resolves the next URL based on the current URL. It always appends unless you start the new URL reference with a leading slash and count from the root.

You will need to alter the URL in the HTML code to point to the correct place.

You want a rewrite? Remove the R=301 here.

wavygravy2k

11:15 pm on Oct 27, 2011 (gmt 0)

10+ Year Member



The HTML form didn't have an action attribute. Maybe this is what I needed.

<form action="search.php"...

wavygravy2k

5:10 pm on Oct 28, 2011 (gmt 0)

10+ Year Member



Some URLs stay the same while others are switched to the "ugly" URL. It must be some sort of caching issue with Firefox because in IE every link stays the same.