Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule - RewriteCond not working

         

acerpro

9:18 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



I have read a number of the posts in this forum and read the docs regarding RewriteRule - RewriteCond, but I can't get this to work.

Incoming URL:

http://www.example.com/my/app?id=20&dn5=abc&attr=xyz

Need to rewrite URL to:

http://www.example.com/my/app?id=20&dn1=abc&attr=xyz

So, I just want to replace the dn5 with dn1.

In my .htaccess I have tried:

RewriteEngine On

RewriteCond %{QUERY_STRING} (.*)dn5=(.*)
RewriteRule ^/my/app$ /my/app?%1dn1=%2 [L]

Obviously this isn't working. I have played with using the R flag as well.

g1smd

10:21 pm on Jun 26, 2009 (gmt 0)

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



By "not working" you get a 404?

Remove the leading / from the RewriteRule pattern if this is being used in .htaccess.

The .* patterns are very inefficient, making your server try a large number of back-off and retry operations. There will be better things to replace the .* with.

acerpro

10:41 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



When I say "not working" I mean that the rules are not matching and thus being ignored. I have removed the leading "/" and it still does not work.

g1smd

10:47 pm on Jun 26, 2009 (gmt 0)

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



Flush browser cache and try again. Your browser cache will hold the earlier incorrect response until you flush it.

acerpro

11:53 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



I have changed my .htaccess to the following and have cleared my browser's cache. It still does not work... My application still only sees the dn5 attribute instead of dn1.

RewriteCond %{QUERY_STRING} (.*)dn5=(.*)
RewriteRule ^my/app$ /my/app?%1dn1=%2 [L]

Any other ideas?

Thanks for the help!

g1smd

12:02 am on Jun 27, 2009 (gmt 0)

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



I am not at all happy with the use of the (.*) patterns as I mentioned above. I would change those. Since I don't know the range of attributes nor values I can't suggest what to change them to.

jdMorgan

1:08 am on Jun 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's get those (.*) subpatterns out of the way:

RewriteCond %{QUERY_STRING} ^(([^&]+&)*)dn5=([^&]+)((&[^&]+)*)$
RewriteRule ^my/app$ /my/app?%1dn1=%3%4 [L]

This pattern will accept any number of name/value pairs before or after the "dn5=<val>"

Assuming you're not getting 500-Server Error, that this code is located in the root .htaccess file at example.com/.htaccess, that you've got MultiViews (content negotiation) disabled, AcceptPathInfo Off, all browser and network caches have been flushed so that the request reaches your server, and that the URL you are requesting is example.com/my/app?blah&dn5=<value>&blah, then this rule will rewrite to /my/app?blah&dn1=<value>&blah

Jim

[edited by: jdMorgan at 1:09 am (utc) on June 27, 2009]