I am trying to match a value in a cookie. The problem is, Apache makes the value url-encoded. So, if I do this:
RewriteCond %{HTTP_COOKIE} ^(.+)$ [NC]
It will capture this:
session%3DeXnR1oDL1Reb8Z3Gdgk7Sg%26account%3D2%3B
instead of this:
session=eXnR1oDL1Reb8Z3Gdgk7Sg&account=2
So there is no way to get the account number to do this:
RewriteRule ^$ /accounts/%1/ [R=301,L]
Please help! I have looked everywhere on Google and stackoverflow and no one has addressed this issue. Thank you so much.
I never did find the answer -- BUT, I did find a work-around. For those having the same problem, here it is:
Separate your sub-cookie values using a character that will NOT get URL encoded. I used the dash character "-". e.g. "VAL1--VAL2".
Good luck!
Did you try using flag -> NE|noescape ?
By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.
RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]
The above example will redirect /anchor/xyz to /bigpage.html#xyz. Omitting the [NE] will result in the # being converted to its hexcode equivalent, %23, which will then result in a 404 Not Found error condition.
Source: http://httpd.apache.org/docs/2.3/rewrite/flags.html
Related
Having a hard time matching this pattern. The URL has only a query string, there is no web page specified.
Here is the URL to be rewritten:
http://foo.com/?page=Target&q=1&id=b9dc586
This is the URL it should be rewritten too:
http://foo.com/target.php&q=1&id=b9dc586
Thanks in advance,
George
Have you tried something like this?
RewriteRule ^?page=Target&q=([0-9]*)&id=([a-f0-9]*) target.php&q=$1&id=$2
Anyway I think the first '&' in your target URL should be a '?', in that case change the rule in
RewriteRule ^?page=Target&q=([0-9]*)&id=([a-f0-9]*) target.php?q=$1&id=$2
I would like to rewrite my url's, so that whenever there's a ?, it and everything after is removed.
I have various strings, such as:
....html?frame=...
....html?sport=...
....html?type=...
So to make it easy, if there's a ? anywhere in the url, it should be truncated.
(By the way, I am already rewriting .php as .html)
Thanks a lot.
As pointed out in the comments:
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ /$1?
Works, but OP says it reverts the php -> html extension conversion back.
Try it adding the php/html rewrite before:
RewriteRule ^(.*)\.php$ /$1.html
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ /$1? [L,R=302]
I just tried it, and I'm having some issues with RewriteBase, which you might need to setup for this to work. Otherwise, looks fine.
From mod_rewrite documentation (emphasis mine):
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
I need to change urls like this one http://outis-music.com/#url=http%3A//outis-music.com/podcast-2/ to eliminate this part /#url=http%3A//outis-music.com
Thank you in advance!
untested code, however you are just running a regular expression search and replace on the url string.
RewriteRule (\/#url=http%3A\/\/outis\-music.com)$ [L]
good reading:
-mod_rewrite sytax
-Regex Reference
I'm trying to remove query strings from my calendar, but my mod_rewrite is not appending the query string.
The website is http://cacrochester.com/Calendar
and if you click the link to go to a different month, the query string is usually http://cacrochester.com/Calendar?currentmonth=2010-11
With my rule below, it just doesn't append the query string so when you click the next month link, it just stays on the month October. What's wrong with my rule?
Here is my rule
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^.*$ http://cacrochester.com/Calendar? [NC,R=301,L]
EDIT:
What i want is to take a url like http://cacrochester.com/Calendar?currentmonth=2010-11 and turn it into something like http://cacrochester.com/Calendar/2010-11
You probably need your app to output relative urls like "/Calendar/2010-11". That's a simple code change.
Then in Apache you'd want to rewrite those urls, using:
RewriteRule ^/Calendar/([0-9]+-[0-9]{2})$ /Calendar.php?currentmonth=$1 [NC,QSA,L]
(You don't want a RewriteCond for this rule.)
Forcing a redirect with R=301 will only expose the internal url scheme. I don't think that's what you want.
To maintain query strings when rewriting, use the QSA (query string append) flag.
[NC,R=301,QSA,L]
In ISAPI_rewrite 3 documentation is this example
RewriteRule ^(.*?\.asp)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,LP,QSA]
http://www.example.com/foo.asp/a/A/b/B/c/C
http://www.example.org/foo.asp?a=A&b=B&c=C
Since my products can have more or less parameters in query string, this seemed like right approach. Based on given example I tried to create a rule for my case with no luck.
http://www.example.com/product-name.aspx/a/A/b/B/c/C
http://www.example.org/products.aspx?a=A&b=B&c=C
After more digging I came up with solution. Seems like there is no other way but using two rules.
RewriteRule ^(.*?\.htm)/([^/]*)/([^/]*)(.+)? $1$4?$2=$3 [NC,LP,QSA]
RewriteRule ^.*?\.htm$ product.aspx [NC,L]