Author Topic: A HTTACCESS challenge for Webmeisters!  (Read 1038 times)

Manotea

  • Where there is doubt...
A HTTACCESS challenge for Webmeisters!
« on: 15 May, 2020, 09:41:22 am »
Hi chaps,

A httaccess challenge for the webmeisters....

I have a URL that looks like: example.com/foo/?parm1=xxxxx.

I want to rewrite it such that if  /?parm1 is supplied it becomes example.com/foo/bah/?parm2=xxxxx

If /?parm1 is not supplied then it should remain as example.com/foo.

So the aim is, if Parm1 is supplied, to inject a folder into the path and rename Parm1 to Parm2

Bonus points for making the URL rewrite 'invisible', i.e., the user sees the original URL only

I tried Stack Overflow but the only responses I received were to correct my grammar and an instruction to RTFM.  ::-)

Ta

Re: A HTTACCESS challenge for Webmeisters!
« Reply #1 on: 15 May, 2020, 10:40:24 am »
Something like:

RewriteRule    ^?parm1=xxxxx$    bah/?parm2=xxxxx     [NC,L]

Should only do the rewrite if it there is a match on "parm1=xxxxx"in the url and shouldn't change the url in the browser.

This assumes that xxxx is always what is supplied with parm1.
Is parm1 a variable ie can it be any four characters instead of xxxx?
I think you'll find it's a bit more complicated than that.

Re: A HTTACCESS challenge for Webmeisters!
« Reply #2 on: 15 May, 2020, 11:40:35 am »
This answer covers the same thing:
https://stackoverflow.com/questions/7234005/rewritecond-for-url-with-parameters

Basically you need a rewritecond for just the subset of urls you want to worry about, a rewritecond to test for the presence of the parameter, and finally a rule that does the rewriting.

Completely untested, and it's been literally years since I did this stuff in anger:

RewriteCond %{REQUEST_URI} ^/foo/
RewriteCond %{QUERY_STRING} ^parm1=(.*)
RewriteRule (.*) /foo/bah/?parm2=%1?

The above probably won't work if you're expecting the incoming URL to contain more than one parameter.

FifeingEejit

  • Not Small
Re: A HTTACCESS challenge for Webmeisters!
« Reply #3 on: 15 May, 2020, 12:09:25 pm »
I tried Stack Overflow but the only responses I received were to correct my grammar and an instruction to RTFM.  ::-)

well.... at least you're not being asked WTF are you using ColdFusion  :sick: and then being given answers on how to do it in Perl.



Manotea

  • Where there is doubt...
Re: A HTTACCESS challenge for Webmeisters!
« Reply #4 on: 24 May, 2020, 12:10:20 pm »
This answer covers the same thing:
https://stackoverflow.com/questions/7234005/rewritecond-for-url-with-parameters

Basically you need a rewritecond for just the subset of urls you want to worry about, a rewritecond to test for the presence of the parameter, and finally a rule that does the rewriting.

Completely untested, and it's been literally years since I did this stuff in anger:

RewriteCond %{REQUEST_URI} ^/foo/
RewriteCond %{QUERY_STRING} ^parm1=(.*)
RewriteRule (.*) /foo/bah/?parm2=%1?

The above probably won't work if you're expecting the incoming URL to contain more than one parameter.

Thanks all... this looks like it ought to work yet doesnt... I suspect other rules are getting in the way. Need to experiment more or failing that, RTFM :(