Jump to content

ReWrite querystring problem


sotusotusotu

Recommended Posts

Hi all,

 

I'm trying to re-write this:

 

www.domain.com/search?={SEARCH WORDS HERE}

 

to:

 

www.domain.com/rewrite_files/search.php?q={SEARCH WORDS HERE}

 

This is what I have written so far but no luck:

 

RewriteRule ^search?([^?q=]+) rewrite_files/search.php?q=$1 [NC,Last]

 

Can anyone please help?

 

Thanks

 

 

 

Link to comment
Share on other sites

Thanks cags but I get:

 

The requested URL /search.php was not found on this server.

 

Note: I am using the following ReWrite code at the top of my .htaccess file in order to remove the .php from my pages:

 

RewriteEngine On
Options All -Indexes
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [R=301,NC]

 

I don't think this should make any difference to the 404 error though.

 

Any ideas?

Link to comment
Share on other sites

Ok, I see two problems. Firstly that doesn't actually match your example, it's probably just a typo but your example didn't have a q in it. Secondly I'm being an idiot. RewriteRule will not match any part of the query string. Anything after the question mark is the query string.

 

Try something more like this instead. The two key points are it's checking the query string contains q={something}, it uses the QSA flag to attach the query string to the new URL.

 

RewriteCond %{QUERY_STRING} q=[^&]+
RewriteRule ^search/?$ /rewrite_files/search.php [QSA,NC,L]

Link to comment
Share on other sites

Appologies for the typo.

 

No joy I'm afraid - still the same 404 error.

 

Just to clarify I want my ReWrite to be: www.domain.com/search?=hello

 

No curly brackets, I was just using these in my example.

 

I did try removing the curly brackets like this:

 

RewriteCond %QUERY_STRING q=[^&]+
RewriteRule ^search/?$ rewrite_files/search.php [QSA,NC,L]

 

No joy again.

Link to comment
Share on other sites

This is a technical subject. Accuracy is everything. Yet again you have given an example without the q, is it supposed to be there or not? What you have posted is so far as I know an invalid URL. The curly braces are supposed to be there, the whole point of that RewriteCond is that you wish to compare against the QUERY_STRING, that is the syntax used. You appear to have changed the rewritten path to not include a forward slash at the start of it, this is quite likely the reason you are getting a 404 error. Since you seemingly haven't used the RewriteBase directive it is probably looking in the wrong place. Adding the R flag should give you a good idea of why it isn't working as it will redirect the page and you can see if it's actually pointing to a file that exists.

Link to comment
Share on other sites

Appologies again, the q is supposed to be there.  You're right this does sound as though it could be a bit technical for me.

 

The whole reason why I wanted to use the this method is that my original method ('www.domain.com/search/keywords') does not like special characters (&, #, +, etc) or encoded characters ('www.domains.com/search/1%261'). Do you know if there is a reason why the htaccess file does not seem to like these characters in the URI?

 

I'm using:

RewriteRule ^search/([^/]+) rewrite_files/search.php?result=$1 [NC,Last]

 

Thanks for all you help.

Link to comment
Share on other sites

All the characters you listed are 'special characters' for URIs, they are reserved characters that have a special meaning in a URI. For example & is for adding a second GET parameter, + is an encoded space etc. These character's cannot be just typed into an address bar and be expected to perform as a literal character. The solution is to use urlencode'd characters, of which %26 is obviously one. There is no reason why this won't work, if you can't get it working then your problem actually lies elsewhere.

 

The problem you will probably have encountered is if you are using %26 as your test case. This is an ampersand (&) which is not a HTML safe character and would probably need to be passed through something like htmlentities before it can safely be echo'd out, otherwise if the following character is a letter it will 'break'.

Link to comment
Share on other sites

Yes I understand what you are saying, this is why I'm encoding the 'special characters' in the URI.

 

The reason I believe it is the .htaccess file that is causing me not to be able to display my result is because if I disable the .htaccess file and go straight to my search page (in this case rewrite_files/search.php?result=1%261) it all works fine using 'echo urldecode($_GET['result']);'.

 

It's a strange one - unless you can think of anything else it would be?

Link to comment
Share on other sites

Just one more thing, I just tried enabling the .htacess and went to /rewrite_files/search.php?result=1%261 andd it worked.  This leads me to think it could be something wrong with the following lines:

 

RewriteEngine On

Options All -Indexes

RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

 

# Return 404 if original request is /foo/bar.php

RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"

RewriteRule .* - [R=301,NC]

Link to comment
Share on other sites

With regards to your first post you could possibly play around with the NE flag (no-escape), other than that I don't know. As for the second post, I don't know where you got the code from, but I don't really understand the objective. The first line I assume is supposed to append .php to the end of the file if there is no file extension? Not sure why you have the question mark in the character class as this wouldn't be a valid URI character anyway. The second little snippet has spaces in the Regex pattern of the RewriteCond, which are again not valid URI characters. I've also never seen a RewriteRule that redirects to -, I won't say that it's wrong because it may just be something I've never seen, but it seems weird to me, what page do you expect it to redirect to?

Link to comment
Share on other sites

I found the code a few month's back.  It was an example of how to strip the '.php' extension from my file names.  It works fine on every page other than when using special characters in my search.

 

Here's the original code with comments:

 

RewriteEngine On
Options All -Indexes
# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [R=301,NC]

 

I've also just tried removing code in my .htaccess file and just use the basic:

 

RewriteEngine on
RewriteRule search/(.*) rewrite_files/search.php?result=$1	

 

No joy again - pulling my hair out :(

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.