I need to convert the following URL structure:
http://www.example.com/forums/attachments/peasgai-emagazine/277230d1518788091-peasgai-emagazine-february-2018-peasgai-emagazine-feb-2018.pdf
to the following:
https://www.example.com/community/attachments/277230/
So, basically, the first numbers till the first letter (277230 d1518788091) are an ID I have to convert to the new URL format.
Any suggestions?
RewriteRule ^attachments/[^/]+/([0-9]+)d[0-9]+-.+$ /community/attachments/$1/ [R=301,L]
Made the trick
Related
I'm trying to rewrite URLs such as
/product/16/var1/value1/var2/value2...
to this
index.php?page=product&id=16&var1=value1&var2=value2...
In other words, I would like to have a "main parameter" translated to an id (and I can do this), but I would also like to have, from that point on, couples of "directories" translated recursively to key-value pairs.
Is this possible with Apache mod_rewrite?
In the absence of the [L] flag, any mod_rewrite rule will apply repeatedly to any URI which corresponds to the rule's rewrite conditions and pattern.
Knowing this, we can build a mod_rewrite rule which looks for any URIs with query strings beginning in a certain way and then repeatedly harvests the folder-names of that URI (two at a time) to build the rest of the query string.
See example below:
In the root folder of
http://example.com/
save an .htaccess file with the following mod_rewrite directives:
RewriteEngine On
RewriteRule ^(product)/([0-9]{2})/(.*) http://%{HTTP_HOST}/$3/index.php?page=$1&id=$2
RewriteCond %{QUERY_STRING} ^(page=product&id=[0-9]{2}.*)
RewriteRule ^([^/]+)/([^/]+)/(.*/)?index.php$ http://%{HTTP_HOST}/$3index.php?%1&$1=$2
Using the above:
http://example.com/product/16/var1/value1/var2/value2/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2
and
http://example.com/product/16/var1/value1/var2/value2/var3/value3/var4/value4/
becomes
http://example.com/index.php?page=product&id=16&var1=value1&var2=value2&var3=value3&var4=value4
Hi all,
Now I want to use mod_rewrite module in apache2 to redirect url.
The rewrite rule looks like:
RewriteCond %{QUERY_STRING} ^url=(.+)$
RewriteRule ^/redir$ %1 [R=301,L]
However, when http://website.com/redir?url=http%3A%2F%2Fwww.google.com is input, the mod_rewrite module cannot unecsape the url parameter http%3A%2F%2Fwww.google.com, is there any method to resolve this problem?
RewriteMap unescape int:unescape
RewriteCond %{QUERY_STRING} ^url=(.+)$
RewriteRule ^/redir$ ${unescape:%1} [R=301,L]
Apache lets you define custom rewrite mappings from different types of external sources. For example, if you wanted to rewrite /users/<some alias> to /users/<full name>, you could have a text file that specified alias/name pairs, and a rewrite rule that translated the "alias" part of the URL using that mapping.
Mappings can come from multiple types of sources. The alias/name example is the standard plain text (txt) type.
RewriteMap also lets you map to a handful of special internal sources (int). They just pass the value to an internal Apache function and return the result. They are:
toupper: Converts the key to all upper case.
tolower: Converts the key to all lower case.
escape: Translates special characters in the key to hex-encodings.
unescape: Translates hex-encodings in the key back to special characters.
unescape is what you're looking for.
More information can be found in the mod_rewrite documentation.
Yep, there is one method: give it to a Php file then make a redirection in Php with appropriate "header".
Something like:
RewriteCond %{QUERY_STRING} ^url=(.+)$
RewriteRule ^/redir$ /myredir.php?redir=%1 [R=301,L]
And in Php, in the file myredir.php something like:
<?php
if (isset($_GET['redir'])) {
header("Location: ".urldecode($_GET['redir']));
}
exit;
?>
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
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]