RewiteRule with question marks to different url's

Alles, was den Apache betrifft, kann hier besprochen werden.

RewiteRule with question marks to different url's

Postby chomps » 14. February 2017 13:04

Hi All,

I am trying to use the RewriteRule correctly but cant seem to get it right. I know the RewriteRule has been discussed many times on this forum but none quite like what I am trying to do. I am using my apache2 vhost file and not .htaccess.
I would like to redirect the following requests only to strip away the ?
http://example.com/?advert_state=1 --> /property-for-sale
http://example.com/?advert_state=2 --> /property-for-rent

So if the url has state=1 strip the ? redirect to the correct url and the same for state=2. I have tried to escape the ? like this \? but that didn't work. I read somewhere else to use [?] to escape a regular expression too. This works but always goes to the second url. My rule currently is as follows:

RewriteEngine On
RewriteRule ^/[?]advert_state=1 /property-for-sale [R=301,L]
RewriteRule ^/[?]advert_state=2 /property-for-rent [R=301,L]

This always goes to /property-for-rent and ignores the first one. I would really appreciate any help.
Regards
chomps
 
Posts: 6
Joined: 13. December 2016 13:27
XAMPP version: 2.4.7
Operating System: Linux

Re: RewiteRule with question marks to different url's

Postby Nobbie » 14. February 2017 13:54

chomps wrote:I have tried to escape the ? like this \? but that didn't work


Anyway, the question mark has to be esacaped via backslash, if you are looking for a question mark. Otherwise question mark is a meta character and means "0 or 1 times the previous character". I think your problem is due to the fact, that the query part of an URL usually is NOT(!) passed to the RewriteRule pattern. You may either use the system variable QUERY_STRING instead (mostly in a RewriteCond, that would do the job here as well) and or use the QSA Flag in the RewriteRule (in order to avoid doubled query variables).

See for example https://wiki.apache.org/httpd/RewriteQueryString

and also see

https://httpd.apache.org/docs/2.4/rewrite/flags.html
Nobbie
 
Posts: 9061
Joined: 09. March 2008 13:04

Re: RewiteRule with question marks to different url's

Postby chomps » 14. February 2017 14:29

Hi Nobbie,

Thank you for your reply. I have tried this. Maybe my syntax is wrong. I have the following:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(\?advert_state=[1-2]$)
RewriteRule ^/\?advert_state=1$ /property-for-sale [R=301,QSA]
RewriteRule ^/\?advert_state=2$ /property-for-rent [R=301,QSA]

I have escaped all the ? . example.com/?advert_state=1 & 2 still only go to the second url and appends the "?advert_state=x" to the end, ie example.com/property-for-rent?advert_state=1
chomps
 
Posts: 6
Joined: 13. December 2016 13:27
XAMPP version: 2.4.7
Operating System: Linux

Re: RewiteRule with question marks to different url's

Postby chomps » 14. February 2017 14:33

I don't think the QSA flag is right as according to the doc - "QSA is to keep the existing query string" which I don't want.
I need to strip away "?advert_state=1" and replace it with "property-for-sale"
and if "?advert_state=2" replace it with "property-for-rent"
chomps
 
Posts: 6
Joined: 13. December 2016 13:27
XAMPP version: 2.4.7
Operating System: Linux

Re: RewiteRule with question marks to different url's

Postby Nobbie » 14. February 2017 15:01

chomps wrote:I don't think the QSA flag is right as according to the doc - "QSA is to keep the existing query string" which I don't want.


Maybe, thats what i said, it is to avoid doubled paramaters.

chomps wrote:I need to strip away "?advert_state=1" and replace it with "property-for-sale"
and if "?advert_state=2" replace it with "property-for-rent"


I know, but you did not solve it correctly. A hint: use two rewriteconds, one for state=1 and one for state=2. And dont forget (i wrote it, but you did not get it), the query part is NOT part of the RewriteRule. There is NO "?advert_state=1" pattern. You have to evaluate that in the RewriteCond (you wasted that, because you handled both values in one condition). This is by far more than i usually would go for a help. you have to learn it, not me. I am not going to solve it for you (its only a small step from here).
Nobbie
 
Posts: 9061
Joined: 09. March 2008 13:04

Re: RewiteRule with question marks to different url's

Postby chomps » 15. February 2017 20:59

"i wrote it, but you did not get it" - to vague to see.
"you have to learn it, not me" - very poor teacher
The reason we come here is to learn how to do something. If you want to hold back and comae from round about angles then don't answer. Rather let someone who is willing to teach, teach.
Show me where I went wrong so that I can understand what I did wrong, and then maybe I can help someone else in the future who actually needs to understand.
Thanx but I will go somewhere else where people are willing to help...
chomps
 
Posts: 6
Joined: 13. December 2016 13:27
XAMPP version: 2.4.7
Operating System: Linux

Re: RewiteRule with question marks to different url's

Postby Nobbie » 15. February 2017 23:17

chomps wrote:Thanx but I will go somewhere else where people are willing to help...


Thats not what want, you want a working solution and you dont want to think, neither to learn. I already gave you very needfull hints and help, but you want more. Good luck somewhere else.
Nobbie
 
Posts: 9061
Joined: 09. March 2008 13:04


Return to Apache

Who is online

Users browsing this forum: No registered users and 4 guests