I need help writing a mod rewrite rule to change the name of a query string parameter. I want to change the name, not the value.
old name partner
new name a_aid
so a link like this
http://domain.com/?partner=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo
will become
http://domain.com/?a_aid=derphipster&pname=foo&plink=http%3A%2F%2Fbar.com%2Ffoo
I found this article but the accepted answer generated errors for the OP:
mod_rewrite - old parameter name to new name
also this article, but the solution was to use PHP. which will not work in my case:
APACHE mod_rewrite change variable name in query string
I can't use PHP because some affiliate tracking code creates a cookie from the query string--and expects the a_aid. So I'm trying to convert partner into a_aid for it
OK think I hacked it together on my own. Please post an answer if you think its brittle or could be done better and I'll accept yours instead
RewriteCond %{QUERY_STRING} ^(.*)partner(.*)$
RewriteRule ^(.*)$ $1?%1a_aid%2 [R=301,L]
Related
I have some problems with my URL rewriting.
I just want to change my URL paradigm's who is :
http://www.siteadress.com/index.php?//something/something to something like that :
http://www.siteadress.com/something/something
So I add this line :
RewriteRule (.?)index.php\?/(.*) /$1$2
But that does'nt work as I planned.
That just remove 1 / from my URL.
Yet in my meaning, I ask for (.?) anything optionnal index.php\?/ index.php?// (.*) and anything, no?
Thank you for your help.
It looks like you have the rule in reverse order. The first argument should be the pattern you're looking to match, the second argument being what you want to replace it to. In your case, you're looking for "/something/something" and wanting to rewrite that to "/index.php?//something/something". So you need something like:
RewriteRule ^/?(.*) /index.php?//$1
That will take anything after the first / in the path and append it to /index.php?//.
I have looked for the answer to my problem here, but the solution is always provided without explaining how to do it, that's the reason I can not do it properly.
I have this code:
RewriteRule ^dex/([^_]*)/([^_]*)/([^_]*)/([^_]*)/([^_]*)$ /dex.php?one=$1&two=$2&three=$3&four=$4&five=$5 [L]
This htaccess as it is makes it mandatory that all parameters are given. This URL works:
http://example.com/dex/one/two/three/four/five
I also want to make it work like this:
http://example.com/dex/one/two/three/four
Making the last (or some) parameters optional. I read something about QSA|qsappend here: http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsa but I can't understand it completely.
Any help? Thank you
To anyone having the same issue, this is the code used to fix it:
RewriteRule ^dex/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?([^/]*)?$ /dex.php?one=$1&two=$2&three=$3&four=$4&five=$5 [L]
Changed ([^_]*) to ([^/]*) and added ? after what I wanted to make optional.
In this case: /? is making a end slash optional, and ([^/]*)? is making the last parameter optional. So it works when the URL is like this:
http://example.com/dex/one/two/three/four/
http://example.com/dex/one/two/three/four
http://example.com/dex/one/two/three/four/five
http://example.com/dex/one/two/three/four/five/
Hope this helps someone.
Rewrite rules use regular expressions. These regular expressions are always tiresome and difficult to maintain. The following, for example, is an answer to question about regex parsers on HTML, and the difficulty you may find with them:
RegEx match open tags except XHTML self-contained tags
Your htaccess file is an Apache configuration file. It should be used for simple configuration, and not for programming. Have it point to the code, and then let the code do the rest. For example, your .htaccess file:
RewriteRule ^(.*)$ index.php [QSA,NC,L]
And, if you're using PHP with index.php...
$objects = explode('/', ltrim($_SERVER[REDIRECT_URL], '/'));
print_r($objects); // list of items from URL
You may end up wanting to grab a different SERVER parameter, but you'll want to keep this complicated stuff in the code, not in configuration files.
I'm a photographer, and run a website with thousands of event photos.
Over the years the path to many directories has changed, and, with a new software, the access to individual photos has changed as well.
I get the path changes covered like this (there are many more rules):
RewriteRule ^([0-9]{4})-major-generals-review/?(.*)$ /galleries/trooping-the-colour/$1-major-generals-review/$2 [R=301,NC,L]
RewriteRule ^([0-9]{4})-bcn-challenge/?(.*)$ /galleries/canal-and-waterway-events/$1-bcn-challenge/$2 [R=301,NC,L]
But I also have to replace, on top of the above, access to individual photos.
The "old" form is, to use a concrete example,
2013-bcn-challenge/index.php?`page=11/thumbnails/1305241839185D24543HaraldJoergens_v1.jpg&autoload=1305251137045D25451HaraldJoergens_v1`
and what I need is
galleries/canal-and-waterway-events/2013-bcn-challenge/1305241839185D24543HaraldJoergens_v1-single.php
so there are two replacements needed,
2013-bcn-challenge with /galleries/canal-and-waterway-events/2013-bcn-challenge/
(that part does work), and
index.php?page=11/thumbnails/1305241839185D24543HaraldJoergens_v1.jpg&autoload=1305251137045D25451HaraldJoergens_v1 with
1305241839185D24543HaraldJoergens_v1-single.php
which does not work. My code for the second replacement is
RewriteRule (.*)/index.php?page=[0-9]+/thumbnails/([-_0-9a-zA-Z]+)\.jpg.* $1/$2-single.php [R=301,NC]
on top of the other rules. Unfortunately, it seems to result in an endless loop.
To make matters worse, instead of "/thumbnails/", it could be "/cust_thumbnails/" or "/photos/", and the "autoload" part can be there or not.
Might anyone be able to help me get around the problem? Thanks a lot in advance!
Harald
The problem is that the query string (?page=...) is not part of the request URI, and so cannot be matched in the rewrite pattern.
As such, you will need to check the query string separately:
RewriteCond %{QUERY_STRING} ^page=\d+/((cust_)?thumbnails|photos)/([\w\-]+).jpg [NC]
RewriteRule ^(.*/)?index.php$ /$1%3-single.php? [R=301,NC,L]
Here, we are checking the query string for the presence of page=<num>, cust_thumbnails, thumbnails, photos, and, of course, the name of the photo.
Then, we are redirecting to /<folder-name>/<file-name>-single.php.
Note: In the expression, I have changed [0-9]+ to \d+ and [-_0-9a-zA-Z]+ to [\w\-]+ - these are both shorthand alternatives.
I want to use Apache's mod_rewrite in order to be able to take each folder of a path as a particular query parameter, for example consider the following:
Basic example
Url requested: http://domain.com/shoes/prada/image-1/
Page served: http://domain.com/?cid=shoes&bid=prada&pid=image-1
In this scenario, there are 3 sub-folders requested (/shoes/, /prada/ then image-1), so the first sub-folder is passed in the actual page served as cid, the second as bid and the third as pid.
Full example
However, I would also like it to serve a particular page depending on the number of sub-folders requested, e.g.
Url requested: http://domain.com/shoes/prada/
Page served: http://domain.com/shop.php?cid=shoes&bid=prada
So far all I've managed to find is regex based matching for mod_rewrite but my path's will vary a lot, which is why I would like to have conditions based on the number of folders accessed (please note, I'm not that good with regex - I reckon a wildcard character would help with this, but I wouldn't be sure where to start).
Any help on this would be greatly appreciated! This is pretty long winded, so if you need any more info for clarifying, please let me know!
With a little bit of work I was able to tweak some regex and get a working rule set for what I wanted:
RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/(.)?$ product.php?tid=$1&sid=$2&eid=$3 [L]
RewriteRule ^(.*)/(.*)/(.)?$ brand.php?tid=$1&sid=$2 [L]
RewriteRule ^(.*)/(.)?$ shop.php?tid=$1 [L]
This is a bit different to the example, however it's what I intended for in the first place.
This allows for the rewriting of url's up to four folders deep, with the "name" of each folder being given as a parameter, and each additional level of depth rewriting the url to a separate resource for example:
http://x.com/shoes/prada/2011-high-heels/ -> http://x.com/product.php?tid=shoes&sid=prada&eid=2011-high-heels
Tested on http://martinmelin.se/rewrite-rule-tester/
How can I use mod_rewrite to take http://www.site.com/events?pg=4 and turn it to a clean URL, like so: http://www.site.com/events/4 ?
Thanks for the help!
Something like this should do the job:
RewriteEngine On
RewriteRule ^events/([^/]*)$ /events?pg=$1 [L]
What it's doing is:
Turning on the Rewrite engine
Matching any request that has the form events/something
Storing the 'something' (the brackets indicate that the match should be stored)
Redirecting to the ugly form using the stored variable.
Preventing any further rules from being applied ([L])
Hope this helps.